31 #include "asterisk/res_pjsip.h"
59 dst = ast_sip_security_mechanisms_alloc(n_params);
63 dst->type = src->type;
64 dst->qvalue = src->qvalue;
66 for (i = 0; i < n_params; i++) {
103 if (!security_mechanisms) {
109 ast_sip_security_mechanisms_destroy(mech);
114 static int ast_sip_str_to_security_mechanism_type(
const char *security_mechanism) {
117 if (!strcasecmp(security_mechanism,
"msrp-tls")) {
118 result = AST_SIP_SECURITY_MECH_MSRP_TLS;
119 }
else if (!strcasecmp(security_mechanism,
"sdes-srtp")) {
120 result = AST_SIP_SECURITY_MECH_SDES_SRTP;
121 }
else if (!strcasecmp(security_mechanism,
"dtls-srtp")) {
122 result = AST_SIP_SECURITY_MECH_DTLS_SRTP;
128 static char *ast_sip_security_mechanism_type_to_str(
enum ast_sip_security_mechanism_type mech_type) {
129 if (mech_type == AST_SIP_SECURITY_MECH_MSRP_TLS) {
131 }
else if (mech_type == AST_SIP_SECURITY_MECH_SDES_SRTP) {
133 }
else if (mech_type == AST_SIP_SECURITY_MECH_DTLS_SRTP) {
143 size_t buf_size = 128;
145 char *ret =
ast_calloc(buf_size,
sizeof(
char));
150 if (security_mechanism == NULL) {
155 snprintf(ret, buf_size - 1,
"%s", ast_sip_security_mechanism_type_to_str(security_mechanism->type));
157 snprintf(ret + strlen(ret), buf_size - 1,
";q=%f.4", security_mechanism->qvalue);
161 for (i = 0; i < size; ++i) {
162 snprintf(ret + strlen(ret), buf_size - 1,
";%s",
AST_VECTOR_GET(&security_mechanism->mechanism_parameters, i));
177 if (!security_mechanisms) {
184 for (i = 0; i < vec_size; ++i) {
186 if (security_mechanism_to_str(mech, add_qvalue, &tmp_buf)) {
189 snprintf(ret + strlen(ret),
sizeof(ret) - 1,
"%s%s",
190 tmp_buf, i == vec_size - 1 ?
"" :
", ");
201 struct pjsip_generic_string_hdr *hdr = pjsip_msg_find_hdr_by_name(msg, hdr_name, NULL);
202 for (; hdr; hdr = pjsip_msg_find_hdr_by_name(msg, hdr_name, hdr->next)) {
203 if (value == NULL || !pj_strcmp2(&hdr->hvalue, value)) {
206 if (hdr->next == hdr) {
221 static float parse_qvalue(
const char *q_value) {
223 float ret = strtof(q_value, &end);
225 if (end == q_value) {
228 }
else if (
'\0' != *end) {
231 }
else if (ret > 1.0 || ret < 0.0) {
246 mech = ast_sip_security_mechanisms_alloc(1);
253 type = ast_sip_str_to_security_mechanism_type(tmp);
265 if (!strncmp(param,
"q=", 2)) {
266 mech->qvalue = parse_qvalue(¶m[2]);
267 if (mech->qvalue < 0.0) {
277 *security_mechanism = mech;
280 if (err && (mech != NULL)) {
281 ast_sip_security_mechanisms_destroy(mech);
287 const char *header_name,
int add_qval, pjsip_tx_data *tdata) {
293 static const pj_str_t proxy_require = {
"Proxy-Require", 13 };
294 static const pj_str_t require = {
"Require", 7 };
296 if (!security_mechanisms || !tdata) {
300 if (!strcmp(header_name,
"Security-Client")) {
302 }
else if (strcmp(header_name,
"Security-Server") &&
303 strcmp(header_name,
"Security-Verify")) {
308 add_qvalue = add_qvalue && add_qval;
311 for (i = 0; i < mech_cnt; ++i) {
313 if (security_mechanism_to_str(mech, add_qvalue, &buf)) {
316 ast_sip_add_header(tdata, header_name, buf);
320 if (pjsip_msg_find_hdr_by_name(tdata->msg, &require, NULL) == NULL) {
321 ast_sip_add_header(tdata,
"Require",
"mediasec");
323 if (pjsip_msg_find_hdr_by_name(tdata->msg, &proxy_require, NULL) == NULL) {
324 ast_sip_add_header(tdata,
"Proxy-Require",
"mediasec");
337 if (!security_mechanisms || !hdr) {
341 if (pj_stricmp2(&hdr->name,
"Security-Client") && pj_stricmp2(&hdr->name,
"Security-Server") &&
342 pj_stricmp2(&hdr->name,
"Security-Verify")) {
346 ast_copy_pj_str(buf, &hdr->hvalue,
sizeof(buf));
#define AST_VECTOR_FREE(vec)
Deallocates this vector.
Asterisk main include file. File version handling, generic pbx functions.
int ast_sip_security_mechanism_vector_init(struct ast_sip_security_mechanism_vector *security_mechanisms, const char *value)
Initialize security mechanism vector from string of security mechanisms.
#define AST_VECTOR_APPEND(vec, elem)
Append an element to a vector, growing the vector if needed.
#define ast_strdup(str)
A wrapper for strdup()
int ast_sip_security_mechanisms_to_str(const struct ast_sip_security_mechanism_vector *security_mechanisms, int add_qvalue, char **buf)
Writes the security mechanisms of an endpoint into a buffer as a string and returns the buffer...
void ast_sip_security_mechanisms_vector_copy(struct ast_sip_security_mechanism_vector *dst, const struct ast_sip_security_mechanism_vector *src)
Duplicate a security mechanism.
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)
Add security headers to transmission data.
Structure representing a security mechanism as defined in RFC 3329.
#define AST_VECTOR_INIT(vec, size)
Initialize a vector.
void ast_sip_remove_headers_by_name_and_value(pjsip_msg *msg, const pj_str_t *hdr_name, const char *value)
Removes all headers of a specific name and value from a pjsip_msg.
#define ast_strdupa(s)
duplicate a string in memory from the stack
char * ast_strsep(char **s, const char sep, uint32_t flags)
Act like strsep but ignore separators inside quotes.
char * ast_skip_blanks(const char *str)
Gets a pointer to the first non-whitespace character in a string.
#define ast_calloc(num, len)
A wrapper for calloc()
#define AST_VECTOR_GET(vec, idx)
Get an element from a vector.
int ast_sip_str_to_security_mechanism(struct ast_sip_security_mechanism **security_mechanism, const char *value)
Allocate a security mechanism from a string.
void ast_sip_header_to_security_mechanism(const pjsip_generic_string_hdr *hdr, struct ast_sip_security_mechanism_vector *security_mechanisms)
Append to security mechanism vector from SIP header.
void ast_sip_security_mechanisms_vector_destroy(struct ast_sip_security_mechanism_vector *security_mechanisms)
Free contents of a security mechanism vector.
#define AST_VECTOR_SIZE(vec)
Get the number of elements in a vector.