-
Notifications
You must be signed in to change notification settings - Fork 911
/
sec_agree.c
299 lines (245 loc) · 8.04 KB
/
sec_agree.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
/**
* Copyright (C) 2017 kamailio.org
*
* This file is part of Kamailio, a free SIP server.
*
* Kamailio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* Kamailio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "sec_agree.h"
#include "../../core/str.h"
#include "../../core/parser/msg_parser.h"
#include "../../core/mem/mem.h"
static uint32_t parse_digits(str value)
{
uint32_t ret = 0;
int buf_len = value.len+1;
char* buf = (char*)malloc(buf_len);
if(!buf) {
return ret;
}
memset(buf, 0, buf_len);
memcpy(buf, value.s, value.len);
ret = atoll(buf);
free(buf);
return ret;
}
static void trim_whitespaces(str* string) {
// skip leading whitespace
while(string->len && (string->s[0]==' ' || string->s[0]=='\t' || string->s[0]=='<')){
string->s = string->s + 1;
string->len --;
}
// skip trailing whitespace
while(string->len && (string->s[string->len-1]==' ' || string->s[string->len-1]=='\t')){
string->len--;
}
}
#define SEC_COPY_STR_PARAM(DST, SRC)\
DST.s = shm_malloc(SRC.len);\
if(DST.s == NULL) {\
return -1;\
}\
memcpy(DST.s, SRC.s, SRC.len);\
DST.len = SRC.len;
static int process_sec_agree_param(str name, str value, ipsec_t *ret)
{
trim_whitespaces(&name);
trim_whitespaces(&value);
if(strncasecmp(name.s, "alg", name.len) == 0) {
SEC_COPY_STR_PARAM(ret->r_alg, value);
}
else if(strncasecmp(name.s, "prot", name.len) == 0) {
SEC_COPY_STR_PARAM(ret->prot, value);
}
else if(strncasecmp(name.s, "mod", name.len) == 0) {
SEC_COPY_STR_PARAM(ret->mod, value);
}
else if(strncasecmp(name.s, "ealg", name.len) == 0) {
SEC_COPY_STR_PARAM(ret->r_ealg, value);
}
else if(strncasecmp(name.s, "spi-c", name.len) == 0) {
ret->spi_uc = parse_digits(value);
}
else if(strncasecmp(name.s, "spi-s", name.len) == 0) {
ret->spi_us = parse_digits(value);
}
else if(strncasecmp(name.s, "port-c", name.len) == 0) {
ret->port_uc = parse_digits(value);
}
else if(strncasecmp(name.s, "port-s", name.len) == 0) {
ret->port_us = parse_digits(value);
}
else {
//unknown parameter
}
return 0;
}
static security_t* parse_sec_agree(struct hdr_field* h)
{
int i = 0;
str name = {0,0};
str value = {0,0};
str mechanism_name = {0,0};
security_t* params = NULL;
str body = h->body;
trim_whitespaces(&body);
// find mechanism name end
for(i = 0; body.s[i] != ';' && i < body.len; i++);
mechanism_name.s = body.s;
mechanism_name.len = i;
if(strncasecmp(mechanism_name.s, "ipsec-3gpp", 10) != 0) {
// unsupported mechanism
LM_ERR("Unsupported mechanism: %.*s\n", STR_FMT(&mechanism_name));
goto cleanup;
}
// allocate shm memory for security_t (it will be saved in contact)
if ((params = shm_malloc(sizeof(security_t))) == NULL) {
LM_ERR("Error allocating shm memory for security_t parameters during sec-agree parsing\n");
return NULL;
}
memset(params, 0, sizeof(security_t));
if((params->sec_header.s = shm_malloc(h->name.len)) == NULL) {
LM_ERR("Error allocating shm memory for security_t sec_header parameter during sec-agree parsing\n");
goto cleanup;
}
memcpy(params->sec_header.s, h->name.s, h->name.len);
params->sec_header.len = h->name.len;
// allocate memory for ipsec_t in security_t
params->data.ipsec = shm_malloc(sizeof(ipsec_t));
if(!params->data.ipsec) {
LM_ERR("Error allocating memory for ipsec parameters during sec-agree parsing\n");
goto cleanup;
}
memset(params->data.ipsec, 0, sizeof(ipsec_t));
// set security type to IPSEC
params->type = SECURITY_IPSEC;
body.s=body.s+i+1;
body.len=body.len-i-1;
// get the rest of the parameters
i = 0;
while(i <= body.len) {
//look for end of buffer or parameter separator
if(i == body.len || body.s[i] == ';' ) {
if(name.len) {
// if(name.len) => a param name is parsed
// and now i points to the end of its value
value.s = body.s;
value.len = i;
}
//else - name is not read but there is a value
//so there is some error - skip ahead
body.s=body.s+i+1;
body.len=body.len-i-1;
i=0;
if(name.len && value.len) {
if(process_sec_agree_param(name, value, params->data.ipsec)) {
goto cleanup;
}
}
//else - something's wrong. Ignore!
//processing is done - reset
name.len=0;
value.len=0;
}
//look for param=value separator
else if(body.s[i] == '=') {
name.s = body.s;
name.len = i;
//position saved - skip ahead
body.s=body.s+i+1;
body.len=body.len-i-1;
i=0;
}
//nothing interesting - move on
else {
i++;
}
}
return params;
cleanup:
// The same piece of code also lives in modules/ims_usrloc_pcscf/pcontact.c
// Function - free_security()
// Keep them in sync!
if (params) {
shm_free(params->sec_header.s);
if(params->type == SECURITY_IPSEC && params->data.ipsec) {
shm_free(params->data.ipsec->ealg.s);
shm_free(params->data.ipsec->r_ealg.s);
shm_free(params->data.ipsec->ck.s);
shm_free(params->data.ipsec->alg.s);
shm_free(params->data.ipsec->r_alg.s);
shm_free(params->data.ipsec->ik.s);
shm_free(params->data.ipsec->prot.s);
shm_free(params->data.ipsec->mod.s);
shm_free(params->data.ipsec);
}
shm_free(params);
}
return NULL;
}
static str s_security_client={"Security-Client",15};
//static str s_security_server={"Security-Server",15};
static str s_security_verify={"Security-Verify",15};
/**
* Looks for the Security-Client header
* @param msg - the sip message
* @param params - ptr to struct sec_agree_params, where parsed values will be saved
* @returns 0 on success, error code on failure
*/
security_t* cscf_get_security(struct sip_msg *msg)
{
struct hdr_field *h = NULL;
if (!msg) return NULL;
if (parse_headers(msg, HDR_EOH_F, 0)<0) {
return NULL;
}
h = msg->headers;
while(h)
{
if (h->name.len == s_security_client.len && strncasecmp(h->name.s, s_security_client.s, s_security_client.len)==0)
{
return parse_sec_agree(h);
}
h = h->next;
}
LM_INFO("No security parameters found\n");
return NULL;
}
/**
* Looks for the Security-Verify header
* @param msg - the sip message
* @param params - ptr to struct sec_agree_params, where parsed values will be saved
* @returns 0 on success, error code on failure
*/
security_t* cscf_get_security_verify(struct sip_msg *msg)
{
struct hdr_field *h = NULL;
if (!msg) return NULL;
if (parse_headers(msg, HDR_EOH_F, 0)<0) {
return NULL;
}
h = msg->headers;
while(h)
{
if (h->name.len == s_security_verify.len && strncasecmp(h->name.s, s_security_verify.s, s_security_verify.len)==0)
{
return parse_sec_agree(h);
}
h = h->next;
}
LM_INFO("No security-verify parameters found\n");
return NULL;
}