Asterisk - The Open Source Telephony Project GIT-master-f36a736
Functions | Variables
security_agreements.c File Reference

Interact with security agreement negotiations and mechanisms. More...

#include "asterisk.h"
#include <pjsip.h>
#include "asterisk/res_pjsip.h"
Include dependency graph for security_agreements.c:

Go to the source code of this file.

Functions

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. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
void ast_sip_security_mechanisms_vector_destroy (struct ast_sip_security_mechanism_vector *security_mechanisms)
 Free contents of a security mechanism vector. More...
 
int ast_sip_str_to_security_mechanism (struct ast_sip_security_mechanism **security_mechanism, const char *value)
 Allocate a security mechanism from a string. More...
 
static float parse_qvalue (const char *q_value)
 
static void security_mechanism_destroy (struct ast_sip_security_mechanism *mech)
 
static int security_mechanism_to_str (const struct ast_sip_security_mechanism *security_mechanism, int add_qvalue, char **buf)
 
static struct ast_sip_security_mechanismsecurity_mechanisms_alloc (size_t n_params)
 
static struct ast_sip_security_mechanismsecurity_mechanisms_copy (const struct ast_sip_security_mechanism *src)
 
static int str_to_security_mechanism_type (const char *security_mechanism)
 

Variables

static char * mechanism_str []
 

Detailed Description

Interact with security agreement negotiations and mechanisms.

Author
Maximilian Fridrich m.fri.nosp@m.dric.nosp@m.h@com.nosp@m.mend.nosp@m..com

Definition in file security_agreements.c.

Function Documentation

◆ ast_sip_add_security_headers()

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.

Parameters
security_mechanismsVector of security mechanisms.
header_nameThe header name under which to add the security mechanisms. One of Security-Client, Security-Server, Security-Verify.
add_qvalIf zero, don't add the q-value to the header.
tdataThe transmission data.
Return values
0Success
non-zeroFailure

Definition at line 290 of file security_agreements.c.

291 {
292 struct ast_sip_security_mechanism *mech;
293 char *buf;
294 int mech_cnt;
295 int i;
296 int add_qvalue = 1;
297 static const pj_str_t proxy_require = { "Proxy-Require", 13 };
298 static const pj_str_t require = { "Require", 7 };
299
300 if (!security_mechanisms || !tdata) {
301 return EINVAL;
302 }
303
304 if (!strcmp(header_name, "Security-Client")) {
305 add_qvalue = 0;
306 } else if (strcmp(header_name, "Security-Server") &&
307 strcmp(header_name, "Security-Verify")) {
308 return EINVAL;
309 }
310 /* If we're adding Security-Client headers, don't add q-value
311 * even if the function caller requested it. */
312 add_qvalue = add_qvalue && add_qval;
313
314 mech_cnt = AST_VECTOR_SIZE(security_mechanisms);
315 for (i = 0; i < mech_cnt; ++i) {
316 mech = AST_VECTOR_GET(security_mechanisms, i);
317 if (security_mechanism_to_str(mech, add_qvalue, &buf)) {
318 continue;
319 }
320 ast_sip_add_header(tdata, header_name, buf);
321 ast_free(buf);
322 }
323
324 if (pjsip_msg_find_hdr_by_name(tdata->msg, &require, NULL) == NULL) {
325 ast_sip_add_header(tdata, "Require", "mediasec");
326 }
327 if (pjsip_msg_find_hdr_by_name(tdata->msg, &proxy_require, NULL) == NULL) {
328 ast_sip_add_header(tdata, "Proxy-Require", "mediasec");
329 }
330 return 0;
331}
#define ast_free(a)
Definition: astmm.h:180
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
int ast_sip_add_header(pjsip_tx_data *tdata, const char *name, const char *value)
Add a header to an outbound SIP message.
Definition: res_pjsip.c:2008
#define NULL
Definition: resample.c:96
static int security_mechanism_to_str(const struct ast_sip_security_mechanism *security_mechanism, int add_qvalue, char **buf)
Structure representing a security mechanism as defined in RFC 3329.
Definition: res_pjsip.h:375
#define AST_VECTOR_SIZE(vec)
Get the number of elements in a vector.
Definition: vector.h:609
#define AST_VECTOR_GET(vec, idx)
Get an element from a vector.
Definition: vector.h:680

References ast_free, ast_sip_add_header(), AST_VECTOR_GET, AST_VECTOR_SIZE, buf, NULL, and security_mechanism_to_str().

Referenced by add_outgoing_request_headers(), and add_security_headers().

◆ ast_sip_header_to_security_mechanism()

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.

Parameters
hdrThe header of the security mechanisms.
security_mechanismsVector of security mechanisms to append to. Header name must be one of Security-Client, Security-Server, Security-Verify.

Definition at line 333 of file security_agreements.c.

334 {
335
336 struct ast_sip_security_mechanism *mech;
337 char buf[512];
338 char *hdr_val;
339 char *mechanism;
340
341 if (!security_mechanisms || !hdr) {
342 return;
343 }
344
345 if (pj_stricmp2(&hdr->name, "Security-Client") && pj_stricmp2(&hdr->name, "Security-Server") &&
346 pj_stricmp2(&hdr->name, "Security-Verify")) {
347 return;
348 }
349
350 ast_copy_pj_str(buf, &hdr->hvalue, sizeof(buf));
351 hdr_val = ast_skip_blanks(buf);
352
353 while ((mechanism = ast_strsep(&hdr_val, ',', AST_STRSEP_ALL))) {
354 if (!ast_sip_str_to_security_mechanism(&mech, mechanism)) {
355 AST_VECTOR_APPEND(security_mechanisms, mech);
356 }
357 }
358}
void ast_copy_pj_str(char *dest, const pj_str_t *src, size_t size)
Copy a pj_str_t into a standard character buffer.
Definition: res_pjsip.c:2201
int ast_sip_str_to_security_mechanism(struct ast_sip_security_mechanism **security_mechanism, const char *value)
Allocate a security mechanism from a string.
@ AST_STRSEP_ALL
Definition: strings.h:258
char * ast_strsep(char **s, const char sep, uint32_t flags)
Act like strsep but ignore separators inside quotes.
Definition: utils.c:1835
char * ast_skip_blanks(const char *str)
Gets a pointer to the first non-whitespace character in a string.
Definition: strings.h:161
#define AST_VECTOR_APPEND(vec, elem)
Append an element to a vector, growing the vector if needed.
Definition: vector.h:256

References ast_copy_pj_str(), ast_sip_str_to_security_mechanism(), ast_skip_blanks(), ast_strsep(), AST_STRSEP_ALL, AST_VECTOR_APPEND, and buf.

Referenced by contact_add_security_headers_to_status(), and handle_registration_response().

◆ ast_sip_remove_headers_by_name_and_value()

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.

Parameters
msgPJSIP message from which to remove headers.
hdr_nameName of the header to remove.
valueOptional string value of the header to remove. If NULL, remove all headers of given hdr_name.

Definition at line 203 of file security_agreements.c.

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}
int value
Definition: syslog.c:37

References NULL, and value.

Referenced by add_security_headers().

◆ ast_sip_security_mechanism_vector_init()

int ast_sip_security_mechanism_vector_init ( struct ast_sip_security_mechanism_vector security_mechanism,
const char *  value 
)

Initialize security mechanism vector from string of security mechanisms.

Parameters
security_mechanismPointer to vector of security mechanisms to initialize.
valueString of security mechanisms as defined in RFC 3329.
Return values
0Success
non-zeroFailure

Definition at line 360 of file security_agreements.c.

361{
362 char *val = value ? ast_strdupa(value) : NULL;
363 struct ast_sip_security_mechanism *mech;
364 char *mechanism;
365
367 if (AST_VECTOR_INIT(security_mechanisms, 1)) {
368 return -1;
369 }
370
371 if (!val) {
372 return 0;
373 }
374
375 while ((mechanism = ast_strsep(&val, ',', AST_STRSEP_ALL))) {
376 if (!ast_sip_str_to_security_mechanism(&mech, mechanism)) {
377 AST_VECTOR_APPEND(security_mechanisms, mech);
378 }
379 }
380
381 return 0;
382}
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
void ast_sip_security_mechanisms_vector_destroy(struct ast_sip_security_mechanism_vector *security_mechanisms)
Free contents of a security mechanism vector.
Definition: ast_expr2.c:325
#define AST_VECTOR_INIT(vec, size)
Initialize a vector.
Definition: vector.h:113

References ast_sip_security_mechanisms_vector_destroy(), ast_sip_str_to_security_mechanism(), ast_strdupa, ast_strsep(), AST_STRSEP_ALL, AST_VECTOR_APPEND, AST_VECTOR_INIT, NULL, and value.

Referenced by security_mechanism_handler(), and security_mechanisms_handler().

◆ ast_sip_security_mechanisms_to_str()

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.

Note
The buffer must be freed by the caller.
Parameters
endpointPointer to endpoint.
add_qvalueIf non-zero, the q-value is printed as well
bufThe buffer to write the string into
Return values
0Success
non-zeroFailure

Definition at line 162 of file security_agreements.c.

163{
164 size_t vec_size;
165 struct ast_sip_security_mechanism *mech;
166 char *tmp_buf;
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);
192 if (rc <= 0) {
193 return ENOMEM;
194 }
195 }
196
197 /* ast_str_truncate removes the trailing ", " on the last mechanism */
199
200 return 0;
201}
const char * str
Definition: app_jack.c:147
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:241
static struct ast_threadstorage tmp_buf
Definition: func_strings.c:48
#define MAX_OBJECT_FIELD
Maximum length of an object field name.
Definition: sorcery.h:110
char * ast_str_truncate(struct ast_str *buf, ssize_t len)
Truncates the enclosed string to the given length.
Definition: strings.h:786
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition: strings.h:1139
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
Definition: strings.h:659
Support for dynamic strings.
Definition: strings.h:623
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:941

References ast_free, ast_str_append(), ast_str_create, ast_str_truncate(), ast_strdup, AST_VECTOR_GET, AST_VECTOR_SIZE, buf, MAX_OBJECT_FIELD, NULL, RAII_VAR, security_mechanism_to_str(), str, and tmp_buf.

Referenced by security_mechanism_to_str().

◆ ast_sip_security_mechanisms_vector_copy()

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.

Parameters
dstSecurity mechanism to duplicate to.
srcSecurity mechanism to duplicate.

Definition at line 81 of file security_agreements.c.

83{
84 struct ast_sip_security_mechanism *mech;
85 int i;
86
88 for (i = 0; i < AST_VECTOR_SIZE(src); i++) {
89 mech = AST_VECTOR_GET(src, i);
91 }
92};
static struct ast_sip_security_mechanism * security_mechanisms_copy(const struct ast_sip_security_mechanism *src)

References ast_sip_security_mechanisms_vector_destroy(), AST_VECTOR_APPEND, AST_VECTOR_GET, AST_VECTOR_SIZE, and security_mechanisms_copy().

Referenced by sip_contact_status_copy(), and sip_outbound_registration_perform().

◆ ast_sip_security_mechanisms_vector_destroy()

void ast_sip_security_mechanisms_vector_destroy ( struct ast_sip_security_mechanism_vector security_mechanisms)

Free contents of a security mechanism vector.

Parameters
security_mechanismsVector whose contents are to be freed

Definition at line 94 of file security_agreements.c.

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}
static void security_mechanism_destroy(struct ast_sip_security_mechanism *mech)
#define AST_VECTOR_RESET(vec, cleanup)
Reset vector.
Definition: vector.h:625
#define AST_VECTOR_FREE(vec)
Deallocates this vector.
Definition: vector.h:174

References AST_VECTOR_FREE, AST_VECTOR_RESET, and security_mechanism_destroy().

Referenced by ast_sip_security_mechanism_vector_init(), ast_sip_security_mechanisms_vector_copy(), endpoint_destructor(), handle_client_state_destruction(), sip_contact_status_dtor(), sip_outbound_registration_destroy(), and sip_outbound_registration_perform().

◆ ast_sip_str_to_security_mechanism()

int ast_sip_str_to_security_mechanism ( struct ast_sip_security_mechanism **  security_mechanism,
const char *  value 
)

Allocate a security mechanism from a string.

Parameters
security_mechanismPointer-pointer to the security mechanism to allocate.
valueThe security mechanism string as defined in RFC 3329 (section 2.2) in the form <mechanism_name>;q=<q_value>;<mechanism_parameters>
Return values
0Success
non-zeroFailure

Definition at line 242 of file security_agreements.c.

242 {
243 struct ast_sip_security_mechanism *mech;
244 char *param;
245 char *tmp;
246 char *mechanism = ast_strdupa(value);
247 int err = 0;
248 int type = -1;
249
251 if (!mech) {
252 err = ENOMEM;
253 goto out;
254 }
255
256 tmp = ast_strsep(&mechanism, ';', AST_STRSEP_ALL);
258 if (type == -1) {
259 err = EINVAL;
260 goto out;
261 }
262
263 mech->type = type;
264 while ((param = ast_strsep(&mechanism, ';', AST_STRSEP_ALL))) {
265 if (!param) {
266 err = EINVAL;
267 goto out;
268 }
269 if (!strncmp(param, "q=", 2)) {
270 mech->qvalue = parse_qvalue(&param[2]);
271 if (mech->qvalue < 0.0) {
272 err = EINVAL;
273 goto out;
274 }
275 continue;
276 }
277 param = ast_strdup(param);
279 }
280
281 *security_mechanism = mech;
282
283out:
284 if (err && (mech != NULL)) {
286 }
287 return err;
288}
static int tmp()
Definition: bt_open.c:389
static const char type[]
Definition: chan_ooh323.c:109
static struct ast_sip_security_mechanism * security_mechanisms_alloc(size_t n_params)
static int str_to_security_mechanism_type(const char *security_mechanism)
static float parse_qvalue(const char *q_value)
struct ast_vector_string mechanism_parameters
Definition: res_pjsip.h:381
enum ast_sip_security_mechanism_type type
Definition: res_pjsip.h:377
FILE * out
Definition: utils/frame.c:33

References ast_strdup, ast_strdupa, ast_strsep(), AST_STRSEP_ALL, AST_VECTOR_APPEND, ast_sip_security_mechanism::mechanism_parameters, NULL, out, parse_qvalue(), ast_sip_security_mechanism::qvalue, security_mechanism_destroy(), security_mechanisms_alloc(), str_to_security_mechanism_type(), tmp(), type, ast_sip_security_mechanism::type, and value.

Referenced by ast_sip_header_to_security_mechanism(), ast_sip_security_mechanism_vector_init(), and rfc3329_incoming_response().

◆ parse_qvalue()

static float parse_qvalue ( const char *  q_value)
static

Definition at line 225 of file security_agreements.c.

225 {
226 char *end;
227 float ret = strtof(q_value, &end);
228
229 if (end == q_value) {
230 /* Not a number. */
231 return -1.0;
232 } else if ('\0' != *end) {
233 /* Extra character at end of input. */
234 return -1.0;
235 } else if (ret > 1.0 || ret < 0.0) {
236 /* Out of valid range. */
237 return -1.0;
238 }
239 return ret;
240}
char * end
Definition: eagi_proxy.c:73

References end.

Referenced by ast_sip_str_to_security_mechanism().

◆ security_mechanism_destroy()

static void security_mechanism_destroy ( struct ast_sip_security_mechanism mech)
static

◆ security_mechanism_to_str()

static int security_mechanism_to_str ( const struct ast_sip_security_mechanism security_mechanism,
int  add_qvalue,
char **  buf 
)
static

Definition at line 124 of file security_agreements.c.

125{
126 size_t size;
127 int i;
128 int rc = 0;
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
159 return 0;
160}
static char * mechanism_str[]
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:761
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Definition: strings.h:1113

References ast_free, ast_str_append(), ast_str_buffer(), ast_str_create, ast_str_set(), ast_strdup, AST_VECTOR_GET, AST_VECTOR_SIZE, buf, MAX_OBJECT_FIELD, ast_sip_security_mechanism::mechanism_parameters, mechanism_str, NULL, ast_sip_security_mechanism::qvalue, RAII_VAR, str, and ast_sip_security_mechanism::type.

Referenced by ast_sip_add_security_headers(), and ast_sip_security_mechanisms_to_str().

◆ security_mechanisms_alloc()

static struct ast_sip_security_mechanism * security_mechanisms_alloc ( size_t  n_params)
static

Definition at line 33 of file security_agreements.c.

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}
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202

References ast_calloc, ast_free, AST_VECTOR_INIT, ast_sip_security_mechanism::mechanism_parameters, NULL, and ast_sip_security_mechanism::qvalue.

Referenced by ast_sip_str_to_security_mechanism(), and security_mechanisms_copy().

◆ security_mechanisms_copy()

static struct ast_sip_security_mechanism * security_mechanisms_copy ( const struct ast_sip_security_mechanism src)
static

Definition at line 50 of file security_agreements.c.

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++) {
69 }
70
71 return dst;
72}

References ast_strdup, AST_VECTOR_APPEND, AST_VECTOR_GET, AST_VECTOR_SIZE, ast_sip_security_mechanism::mechanism_parameters, NULL, ast_sip_security_mechanism::qvalue, security_mechanisms_alloc(), and ast_sip_security_mechanism::type.

Referenced by ast_sip_security_mechanisms_vector_copy().

◆ str_to_security_mechanism_type()

static int str_to_security_mechanism_type ( const char *  security_mechanism)
static

Definition at line 112 of file security_agreements.c.

112 {
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}
#define ARRAY_LEN(a)
Definition: utils.h:666

References ARRAY_LEN, and mechanism_str.

Referenced by ast_sip_str_to_security_mechanism().

Variable Documentation

◆ mechanism_str

char* mechanism_str[]
static