Coverage Report

Created: 2026-12-01 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/osip/src/osipparser2/osip_authentication_info.c
Line
Count
Source
1
/*
2
  The oSIP library implements the Session Initiation Protocol (SIP -rfc3261-)
3
  Copyright (C) 2001-2020 Aymeric MOIZARD amoizard@antisip.com
4
5
  This library is free software; you can redistribute it and/or
6
  modify it under the terms of the GNU Lesser General Public
7
  License as published by the Free Software Foundation; either
8
  version 2.1 of the License, or (at your option) any later version.
9
10
  This library is distributed in the hope that it will be useful,
11
  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
  Lesser General Public License for more details.
14
15
  You should have received a copy of the GNU Lesser General Public
16
  License along with this library; if not, write to the Free Software
17
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
*/
19
20
#include <osipparser2/internal.h>
21
22
#include <osipparser2/osip_port.h>
23
#include <osipparser2/osip_message.h>
24
#include <osipparser2/osip_parser.h>
25
#include "parser.h"
26
27
#ifndef MINISIZE
28
29
10.5k
int osip_authentication_info_init(osip_authentication_info_t **dest) {
30
10.5k
  *dest = (osip_authentication_info_t *) osip_malloc(sizeof(osip_authentication_info_t));
31
32
10.5k
  if (*dest == NULL)
33
0
    return OSIP_NOMEM;
34
35
10.5k
  memset(*dest, 0, sizeof(osip_authentication_info_t));
36
10.5k
  return OSIP_SUCCESS;
37
10.5k
}
38
39
/* fills the www-authenticate header of message.               */
40
/* INPUT :  char *hvalue | value of header.   */
41
/* OUTPUT: osip_message_t *sip | structure to save results. */
42
/* returns -1 on error. */
43
10.5k
int osip_message_set_authentication_info(osip_message_t *sip, const char *hvalue) {
44
10.5k
  osip_authentication_info_t *authentication_info;
45
10.5k
  int i;
46
47
10.5k
  if (hvalue == NULL || hvalue[0] == '\0')
48
392
    return OSIP_SUCCESS;
49
50
10.1k
  if (sip == NULL)
51
0
    return OSIP_BADPARAMETER;
52
53
10.1k
  i = osip_authentication_info_init(&authentication_info);
54
55
10.1k
  if (i != 0)
56
0
    return i;
57
58
10.1k
  i = osip_authentication_info_parse(authentication_info, hvalue);
59
60
10.1k
  if (i != 0) {
61
3.24k
    osip_authentication_info_free(authentication_info);
62
3.24k
    return i;
63
3.24k
  }
64
65
6.89k
  sip->message_property = 2;
66
67
6.89k
  osip_list_add(&sip->authentication_infos, authentication_info, -1);
68
6.89k
  return OSIP_SUCCESS;
69
10.1k
}
70
71
/* fills the authentication_info strucuture.                      */
72
/* INPUT : char *hvalue | value of header.         */
73
/* OUTPUT: osip_message_t *sip | structure to save results. */
74
/* returns -1 on error. */
75
/* TODO:
76
   digest-challenge tken has no order preference??
77
   verify many situations (extra SP....)
78
*/
79
10.5k
int osip_authentication_info_parse(osip_authentication_info_t *ainfo, const char *hvalue) {
80
10.5k
  const char *space, *hack;
81
10.5k
  const char *next = NULL;
82
10.5k
  int i;
83
84
10.5k
  space = strchr(hvalue, ' ');
85
10.5k
  hack = strchr(hvalue, '=');
86
87
10.5k
  if (space && hack && hack > space) {
88
1.01k
    ainfo->auth_type = (char *) osip_malloc(space - hvalue + 1);
89
90
1.01k
    if (ainfo->auth_type == NULL)
91
0
      return OSIP_NOMEM;
92
93
1.01k
    osip_strncpy(ainfo->auth_type, hvalue, space - hvalue);
94
95
1.01k
  } else
96
9.56k
    space = hvalue;
97
98
18.2k
  for (;;) {
99
18.2k
    int parse_ok = 0;
100
101
18.2k
    i = __osip_quoted_string_set("nextnonce", space, &(ainfo->nextnonce), &next);
102
103
18.2k
    if (i != 0)
104
197
      return i;
105
106
18.0k
    if (next == NULL)
107
502
      return OSIP_SUCCESS; /* end of header detected! */
108
17.5k
    else if (next != space) {
109
2.43k
      space = next;
110
2.43k
      parse_ok++;
111
2.43k
    }
112
113
17.5k
    i = __osip_quoted_string_set("cnonce", space, &(ainfo->cnonce), &next);
114
115
17.5k
    if (i != 0)
116
385
      return i;
117
118
17.2k
    if (next == NULL)
119
402
      return OSIP_SUCCESS; /* end of header detected! */
120
16.8k
    else if (next != space) {
121
860
      space = next;
122
860
      parse_ok++;
123
860
    }
124
125
16.8k
    i = __osip_quoted_string_set("rspauth", space, &(ainfo->rspauth), &next);
126
127
16.8k
    if (i != 0)
128
196
      return i;
129
130
16.6k
    if (next == NULL)
131
222
      return OSIP_SUCCESS; /* end of header detected! */
132
16.3k
    else if (next != space) {
133
564
      space = next;
134
564
      parse_ok++;
135
564
    }
136
137
16.3k
    i = __osip_token_set("nc", space, &(ainfo->nonce_count), &next);
138
139
16.3k
    if (i != 0)
140
1.10k
      return i;
141
142
15.2k
    if (next == NULL)
143
2.30k
      return OSIP_SUCCESS; /* end of header detected! */
144
12.9k
    else if (next != space) {
145
595
      space = next;
146
595
      parse_ok++;
147
595
    }
148
149
12.9k
    i = __osip_token_set("qop", space, &(ainfo->qop_options), &next);
150
151
12.9k
    if (i != 0)
152
387
      return i;
153
154
12.5k
    if (next == NULL)
155
1.19k
      return OSIP_SUCCESS; /* end of header detected! */
156
11.3k
    else if (next != space) {
157
651
      space = next;
158
651
      parse_ok++;
159
651
    }
160
161
11.3k
    i = __osip_quoted_string_set("snum", space, &(ainfo->snum), &next);
162
163
11.3k
    if (i != 0)
164
194
      return i;
165
166
11.1k
    if (next == NULL)
167
398
      return OSIP_SUCCESS; /* end of header detected! */
168
10.8k
    else if (next != space) {
169
816
      space = next;
170
816
      parse_ok++;
171
816
    }
172
173
10.8k
    i = __osip_quoted_string_set("srand", space, &(ainfo->srand), &next);
174
175
10.8k
    if (i != 0)
176
198
      return i;
177
178
10.6k
    if (next == NULL)
179
447
      return OSIP_SUCCESS; /* end of header detected! */
180
10.1k
    else if (next != space) {
181
419
      space = next;
182
419
      parse_ok++;
183
419
    }
184
185
10.1k
    i = __osip_quoted_string_set("targetname", space, &(ainfo->targetname), &next);
186
187
10.1k
    if (i != 0)
188
196
      return i;
189
190
9.96k
    if (next == NULL)
191
222
      return OSIP_SUCCESS; /* end of header detected! */
192
9.73k
    else if (next != space) {
193
418
      space = next;
194
418
      parse_ok++;
195
418
    }
196
197
9.73k
    i = __osip_quoted_string_set("realm", space, &(ainfo->realm), &next);
198
199
9.73k
    if (i != 0)
200
197
      return i;
201
202
9.54k
    if (next == NULL)
203
267
      return OSIP_SUCCESS; /* end of header detected! */
204
9.27k
    else if (next != space) {
205
397
      space = next;
206
397
      parse_ok++;
207
397
    }
208
209
9.27k
    i = __osip_quoted_string_set("opaque", space, &(ainfo->opaque), &next);
210
211
9.27k
    if (i != 0)
212
194
      return i;
213
214
9.08k
    if (next == NULL)
215
438
      return OSIP_SUCCESS; /* end of header detected! */
216
8.64k
    else if (next != space) {
217
405
      space = next;
218
405
      parse_ok++;
219
405
    }
220
221
8.64k
    if (0 == parse_ok) {
222
3.77k
      const char *quote1, *quote2, *tmp;
223
224
      /* CAUTION */
225
      /* parameter not understood!!! I'm too lazy to handle IT */
226
      /* let's simply bypass it */
227
3.77k
      if (strlen(space) < 1)
228
213
        return OSIP_SUCCESS;
229
230
3.56k
      tmp = strchr(space + 1, ',');
231
232
3.56k
      if (tmp == NULL) /* it was the last header */
233
328
        return OSIP_SUCCESS;
234
235
3.23k
      quote1 = __osip_quote_find(space);
236
237
3.23k
      if ((quote1 != NULL) && (quote1 < tmp)) { /* this may be a quoted string! */
238
887
        quote2 = __osip_quote_find(quote1 + 1);
239
240
887
        if (quote2 == NULL)
241
196
          return OSIP_SYNTAXERROR; /* bad header format... */
242
243
691
        if (tmp < quote2) /* the comma is inside the quotes! */
244
389
          space = strchr(quote2, ',');
245
246
302
        else
247
302
          space = tmp;
248
249
691
        if (space == NULL) /* it was the last header */
250
203
          return OSIP_SUCCESS;
251
252
691
      } else
253
2.34k
        space = tmp;
254
255
      /* continue parsing... */
256
3.23k
    }
257
8.64k
  }
258
259
0
  return OSIP_SUCCESS; /* ok */
260
10.5k
}
261
262
/* returns the authentication_info header.            */
263
/* INPUT : osip_message_t *sip | sip message.   */
264
/* returns null on error. */
265
0
int osip_message_get_authentication_info(const osip_message_t *sip, int pos, osip_authentication_info_t **dest) {
266
0
  osip_authentication_info_t *authentication_info;
267
268
0
  *dest = NULL;
269
270
0
  if (osip_list_size(&sip->authentication_infos) <= pos)
271
0
    return OSIP_UNDEFINED_ERROR; /* does not exist */
272
273
0
  authentication_info = (osip_authentication_info_t *) osip_list_get(&sip->authentication_infos, pos);
274
275
0
  *dest = authentication_info;
276
0
  return pos;
277
0
}
278
279
0
char *osip_authentication_info_get_auth_type(osip_authentication_info_t *authentication_info) {
280
0
  return authentication_info->auth_type;
281
0
}
282
283
0
void osip_authentication_info_set_auth_type(osip_authentication_info_t *authentication_info, char *auth_type) {
284
0
  authentication_info->auth_type = (char *) auth_type;
285
0
}
286
287
0
char *osip_authentication_info_get_nextnonce(osip_authentication_info_t *authentication_info) {
288
0
  return authentication_info->nextnonce;
289
0
}
290
291
0
void osip_authentication_info_set_nextnonce(osip_authentication_info_t *authentication_info, char *nextnonce) {
292
0
  authentication_info->nextnonce = (char *) nextnonce;
293
0
}
294
295
0
char *osip_authentication_info_get_cnonce(osip_authentication_info_t *authentication_info) {
296
0
  return authentication_info->cnonce;
297
0
}
298
299
0
void osip_authentication_info_set_cnonce(osip_authentication_info_t *authentication_info, char *cnonce) {
300
0
  authentication_info->cnonce = (char *) cnonce;
301
0
}
302
303
0
char *osip_authentication_info_get_rspauth(osip_authentication_info_t *authentication_info) {
304
0
  return authentication_info->rspauth;
305
0
}
306
307
0
void osip_authentication_info_set_rspauth(osip_authentication_info_t *authentication_info, char *rspauth) {
308
0
  authentication_info->rspauth = (char *) rspauth;
309
0
}
310
311
0
char *osip_authentication_info_get_nonce_count(osip_authentication_info_t *authentication_info) {
312
0
  return authentication_info->nonce_count;
313
0
}
314
315
0
void osip_authentication_info_set_nonce_count(osip_authentication_info_t *authentication_info, char *nonce_count) {
316
0
  authentication_info->nonce_count = (char *) nonce_count;
317
0
}
318
319
0
char *osip_authentication_info_get_qop_options(osip_authentication_info_t *authentication_info) {
320
0
  return authentication_info->qop_options;
321
0
}
322
323
0
void osip_authentication_info_set_qop_options(osip_authentication_info_t *authentication_info, char *qop_options) {
324
0
  authentication_info->qop_options = (char *) qop_options;
325
0
}
326
327
0
char *osip_authentication_info_get_snum(osip_authentication_info_t *authentication_info) {
328
0
  return authentication_info->snum;
329
0
}
330
331
0
void osip_authentication_info_set_snum(osip_authentication_info_t *authentication_info, char *snum) {
332
0
  authentication_info->snum = (char *) snum;
333
0
}
334
335
0
char *osip_authentication_info_get_srand(osip_authentication_info_t *authentication_info) {
336
0
  return authentication_info->srand;
337
0
}
338
339
0
void osip_authentication_info_set_srand(osip_authentication_info_t *authentication_info, char *srand) {
340
0
  authentication_info->srand = (char *) srand;
341
0
}
342
343
0
char *osip_authentication_info_get_targetname(osip_authentication_info_t *authentication_info) {
344
0
  return authentication_info->targetname;
345
0
}
346
347
0
void osip_authentication_info_set_targetname(osip_authentication_info_t *authentication_info, char *targetname) {
348
0
  authentication_info->targetname = (char *) targetname;
349
0
}
350
351
0
char *osip_authentication_info_get_realm(osip_authentication_info_t *authentication_info) {
352
0
  return authentication_info->realm;
353
0
}
354
355
0
void osip_authentication_info_set_realm(osip_authentication_info_t *authentication_info, char *realm) {
356
0
  authentication_info->realm = (char *) realm;
357
0
}
358
359
0
char *osip_authentication_info_get_opaque(osip_authentication_info_t *authentication_info) {
360
0
  return authentication_info->opaque;
361
0
}
362
363
0
void osip_authentication_info_set_opaque(osip_authentication_info_t *authentication_info, char *opaque) {
364
0
  authentication_info->opaque = (char *) opaque;
365
0
}
366
367
/* returns the authentication_info header as a string.          */
368
/* INPUT : osip_authentication_info_t *authentication_info | authentication_info header.  */
369
/* returns null on error. */
370
4.34k
int osip_authentication_info_to_str(const osip_authentication_info_t *ainfo, char **dest) {
371
4.34k
  size_t len;
372
4.34k
  char *tmp, *start;
373
374
4.34k
  *dest = NULL;
375
376
4.34k
  if (ainfo == NULL)
377
0
    return OSIP_BADPARAMETER;
378
379
4.34k
  len = 0;
380
381
4.34k
  if (ainfo->auth_type != NULL)
382
752
    len = len + strlen(ainfo->auth_type) + 1;
383
384
4.34k
  if (ainfo->nextnonce != NULL)
385
409
    len = len + strlen(ainfo->nextnonce) + 12; /* fixed Jan 10,2020: missing one byte */
386
387
4.34k
  if (ainfo->rspauth != NULL)
388
391
    len = len + strlen(ainfo->rspauth) + 10;
389
390
4.34k
  if (ainfo->cnonce != NULL)
391
759
    len = len + strlen(ainfo->cnonce) + 9;
392
393
4.34k
  if (ainfo->nonce_count != NULL)
394
1.11k
    len = len + strlen(ainfo->nonce_count) + 5;
395
396
4.34k
  if (ainfo->qop_options != NULL)
397
1.34k
    len = len + strlen(ainfo->qop_options) + 6;
398
399
4.34k
  if (ainfo->snum != NULL)
400
390
    len = len + strlen(ainfo->snum) + 7;
401
402
4.34k
  if (ainfo->srand != NULL)
403
401
    len = len + strlen(ainfo->srand) + 8;
404
405
4.34k
  if (ainfo->targetname != NULL)
406
402
    len = len + strlen(ainfo->targetname) + 13;
407
408
4.34k
  if (ainfo->realm != NULL)
409
394
    len = len + strlen(ainfo->realm) + 8;
410
411
4.34k
  if (ainfo->opaque != NULL)
412
388
    len = len + strlen(ainfo->opaque) + 9; /* fixed Jan 10,2020: missing one byte */
413
414
4.34k
  if (len == 0)
415
31
    return OSIP_BADPARAMETER;
416
417
4.31k
  len++; /* fixed Jan 10,2020: missing one byte for the final \0 added by osip_str_append */
418
4.31k
  tmp = (char *) osip_malloc(len);
419
420
4.31k
  if (tmp == NULL)
421
0
    return OSIP_NOMEM;
422
423
4.31k
  *dest = tmp;
424
425
4.31k
  start = tmp;
426
427
4.31k
  if (ainfo->auth_type != NULL) {
428
752
    tmp = osip_str_append(tmp, ainfo->auth_type);
429
752
    tmp = osip_str_append(tmp, " ");
430
752
    start = tmp;
431
752
  }
432
433
4.31k
  if (ainfo->qop_options != NULL) {
434
1.34k
    if (tmp != start) {
435
0
      tmp = osip_strn_append(tmp, ", ", 2);
436
0
    }
437
438
1.34k
    tmp = osip_strn_append(tmp, "qop=", 4);
439
1.34k
    tmp = osip_str_append(tmp, ainfo->qop_options);
440
1.34k
  }
441
442
4.31k
  if (ainfo->nextnonce != NULL) {
443
409
    if (tmp != start) {
444
196
      tmp = osip_strn_append(tmp, ", ", 2);
445
196
    }
446
447
409
    tmp = osip_strn_append(tmp, "nextnonce=", 10);
448
409
    tmp = osip_str_append(tmp, ainfo->nextnonce);
449
409
  }
450
451
4.31k
  if (ainfo->rspauth != NULL) {
452
391
    if (tmp != start) {
453
197
      tmp = osip_strn_append(tmp, ", ", 2);
454
197
    }
455
456
391
    tmp = osip_strn_append(tmp, "rspauth=", 8);
457
391
    tmp = osip_str_append(tmp, ainfo->rspauth);
458
391
  }
459
460
4.31k
  if (ainfo->cnonce != NULL) {
461
759
    if (tmp != start) {
462
350
      tmp = osip_strn_append(tmp, ", ", 2);
463
350
    }
464
465
759
    tmp = osip_strn_append(tmp, "cnonce=", 7);
466
759
    tmp = osip_str_append(tmp, ainfo->cnonce);
467
759
  }
468
469
4.31k
  if (ainfo->nonce_count != NULL) {
470
1.11k
    if (tmp != start) {
471
273
      tmp = osip_strn_append(tmp, ", ", 2);
472
273
    }
473
474
1.11k
    tmp = osip_strn_append(tmp, "nc=", 3);
475
1.11k
    tmp = osip_str_append(tmp, ainfo->nonce_count);
476
1.11k
  }
477
478
4.31k
  if (ainfo->snum != NULL) {
479
390
    if (tmp != start) {
480
196
      tmp = osip_strn_append(tmp, ", ", 2);
481
196
    }
482
483
390
    tmp = osip_strn_append(tmp, "snum=", 5);
484
390
    tmp = osip_str_append(tmp, ainfo->snum);
485
390
  }
486
487
4.31k
  if (ainfo->srand != NULL) {
488
401
    if (tmp != start) {
489
206
      tmp = osip_strn_append(tmp, ", ", 2);
490
206
    }
491
492
401
    tmp = osip_strn_append(tmp, "srand=", 6);
493
401
    tmp = osip_str_append(tmp, ainfo->srand);
494
401
  }
495
496
4.31k
  if (ainfo->targetname != NULL) {
497
402
    if (tmp != start) {
498
205
      tmp = osip_strn_append(tmp, ", ", 2);
499
205
    }
500
501
402
    tmp = osip_strn_append(tmp, "targetname=", 11);
502
402
    tmp = osip_str_append(tmp, ainfo->targetname);
503
402
  }
504
505
4.31k
  if (ainfo->realm != NULL) {
506
394
    if (tmp != start) {
507
194
      tmp = osip_strn_append(tmp, ", ", 2);
508
194
    }
509
510
394
    tmp = osip_strn_append(tmp, "realm=", 6);
511
394
    tmp = osip_str_append(tmp, ainfo->realm);
512
394
  }
513
514
4.31k
  if (ainfo->opaque != NULL) {
515
388
    if (tmp != start) {
516
194
      tmp = osip_strn_append(tmp, ", ", 2);
517
194
    }
518
519
388
    tmp = osip_strn_append(tmp, "opaque=", 7);
520
388
    tmp = osip_str_append(tmp, ainfo->opaque);
521
388
  }
522
523
4.31k
  return OSIP_SUCCESS;
524
4.31k
}
525
526
/* deallocates a osip_authentication_info_t structure.  */
527
/* INPUT : osip_authentication_info_t *authentication_info | authentication_info. */
528
10.5k
void osip_authentication_info_free(osip_authentication_info_t *authentication_info) {
529
10.5k
  if (authentication_info == NULL)
530
0
    return;
531
532
10.5k
  osip_free(authentication_info->auth_type);
533
10.5k
  osip_free(authentication_info->nextnonce);
534
10.5k
  osip_free(authentication_info->rspauth);
535
10.5k
  osip_free(authentication_info->cnonce);
536
10.5k
  osip_free(authentication_info->nonce_count);
537
10.5k
  osip_free(authentication_info->qop_options);
538
10.5k
  osip_free(authentication_info->snum);
539
10.5k
  osip_free(authentication_info->srand);
540
10.5k
  osip_free(authentication_info->targetname);
541
10.5k
  osip_free(authentication_info->realm);
542
10.5k
  osip_free(authentication_info->opaque);
543
10.5k
  osip_free(authentication_info);
544
10.5k
}
545
546
0
int osip_authentication_info_clone(const osip_authentication_info_t *ainfo, osip_authentication_info_t **dest) {
547
0
  int i;
548
0
  osip_authentication_info_t *wa;
549
550
0
  *dest = NULL;
551
552
0
  if (ainfo == NULL)
553
0
    return OSIP_BADPARAMETER;
554
555
0
  i = osip_authentication_info_init(&wa);
556
557
0
  if (i != 0) /* allocation failed */
558
0
    return i;
559
560
0
  if (ainfo->auth_type != NULL)
561
0
    wa->auth_type = osip_strdup(ainfo->auth_type);
562
563
0
  if (ainfo->nextnonce != NULL)
564
0
    wa->nextnonce = osip_strdup(ainfo->nextnonce);
565
566
0
  if (ainfo->cnonce != NULL)
567
0
    wa->cnonce = osip_strdup(ainfo->cnonce);
568
569
0
  if (ainfo->rspauth != NULL)
570
0
    wa->rspauth = osip_strdup(ainfo->rspauth);
571
572
0
  if (ainfo->nonce_count != NULL)
573
0
    wa->nonce_count = osip_strdup(ainfo->nonce_count);
574
575
0
  if (ainfo->qop_options != NULL)
576
0
    wa->qop_options = osip_strdup(ainfo->qop_options);
577
578
0
  if (ainfo->snum != NULL)
579
0
    wa->snum = osip_strdup(ainfo->snum);
580
581
0
  if (ainfo->srand != NULL)
582
0
    wa->srand = osip_strdup(ainfo->srand);
583
584
0
  if (ainfo->targetname != NULL)
585
0
    wa->targetname = osip_strdup(ainfo->targetname);
586
587
0
  if (ainfo->realm != NULL)
588
0
    wa->realm = osip_strdup(ainfo->realm);
589
590
0
  if (ainfo->opaque != NULL)
591
0
    wa->opaque = osip_strdup(ainfo->opaque);
592
593
0
  *dest = wa;
594
0
  return OSIP_SUCCESS;
595
0
}
596
597
#endif