File: authorize.c

package info (click to toggle)
kamailio 4.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 56,100 kB
  • sloc: ansic: 552,832; xml: 166,484; sh: 8,659; makefile: 7,676; sql: 6,235; perl: 3,487; yacc: 3,428; python: 1,457; cpp: 1,219; php: 1,047; java: 449; pascal: 194; cs: 40; awk: 27
file content (1687 lines) | stat: -rw-r--r-- 57,825 bytes parent folder | download | duplicates (2)
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
/*
 * $Id$
 *
 * Copyright (C) 2012 Smile Communications, jason.penton@smilecoms.com
 * Copyright (C) 2012 Smile Communications, richard.good@smilecoms.com
 * 
 * The initial version of this code was written by Dragos Vingarzan
 * (dragos(dot)vingarzan(at)fokus(dot)fraunhofer(dot)de and the
 * Fruanhofer Institute. It was and still is maintained in a separate
 * branch of the original SER. We are therefore migrating it to
 * Kamailio/SR and look forward to maintaining it from here on out.
 * 2011/2012 Smile Communications, Pty. Ltd.
 * ported/maintained/improved by 
 * Jason Penton (jason(dot)penton(at)smilecoms.com and
 * Richard Good (richard(dot)good(at)smilecoms.com) as part of an 
 * effort to add full IMS support to Kamailio/SR using a new and
 * improved architecture
 * 
 * NB: Alot of this code was originally part of OpenIMSCore,
 * FhG Fokus. 
 * Copyright (C) 2004-2006 FhG Fokus
 * Thanks for great work! This is an effort to 
 * break apart the various CSCF functions into logically separate
 * components. We hope this will drive wider use. We also feel
 * that in this way the architecture is more complete and thereby easier
 * to manage in the Kamailio/SR environment
 *
 * 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 <string.h>
#include "../../ut.h"
#include "../../str.h"
#include "../../basex.h"
#include "../../hashes.h"
#include "../../lib/srdb1/db.h"
#include "../../lib/srdb1/db_ut.h"
#include "../../dprint.h"
#include "../../parser/digest/digest.h"
#include "../../parser/hf.h"
#include "../../parser/parser_f.h"
#include "../../usr_avp.h"
#include "../../mod_fix.h"
#include "../../mem/mem.h"
#include "../cdp/diameter.h"
#include "../cdp/diameter_ims_code_result.h"

#include "cxdx_mar.h"
#include "cxdx_avp.h"


#include "../../lib/ims/ims_getters.h"
#include "../tm/tm_load.h"
#include "api.h"
#include "authims_mod.h"
#include "authorize.h"
#include "utils.h"
#include "../../action.h" /* run_actions */

extern unsigned char registration_default_algorithm_type; /**< fixed default algorithm for registration (if none present)	*/
extern struct tm_binds tmb;
extern struct cdp_binds cdpb;

extern str registration_qop_str; /**< the qop options to put in the authorization challenges */
extern int av_request_at_sync; /**< how many auth vectors to request in a sync MAR 		*/
extern int av_request_at_once; /**< how many auth vectors to request in a MAR 				*/
extern int auth_vector_timeout;
extern int auth_data_timeout; /**< timeout for a hash entry to expire when empty in sec 	*/
extern int auth_used_vector_timeout;
extern int add_authinfo_hdr;
extern int max_nonce_reuse;
extern str scscf_name_str;
extern int ignore_failed_auth;
extern int av_check_only_impu;

auth_hash_slot_t *auth_data; /**< Authentication vector hash table */
static int act_auth_data_hash_size = 0; /**< authentication vector hash table size */

static str empty_s = {0, 0};

str S_WWW = {"WWW", 3};
str S_Proxy = {"Proxy", 5};
str S_Authorization_AKA = {"%.*s-Authenticate: Digest realm=\"%.*s\","
    " nonce=\"%.*s\", algorithm=%.*s, ck=\"%.*s\", ik=\"%.*s\"%.*s\r\n", 107};
str S_Authorization_MD5 = {"%.*s-Authenticate: Digest realm=\"%.*s\","
    " nonce=\"%.*s\", algorithm=%.*s%.*s\r\n", 102};

str algorithm_types[] = {
    {"unknown", 7},
    {"AKAv1-MD5", 9},
    {"AKAv2-MD5", 9},
    {"Early-IMS", 9},
    {"MD5", 3},
    {"CableLabs-Digest", 16},
    {"3GPP-Digest", 11},
    {"TISPAN-HTTP_DIGEST_MD5", 22},
    {"NASS-Bundled", 12},
    {0, 0}
};

str auth_scheme_types[] = {
    {"unknown", 7},
    {"Digest-AKAv1-MD5", 16},
    {"Digest-AKAv2-MD5", 16},
    {"Early-IMS-Security", 18},
    {"Digest-MD5", 10},
    {"Digest", 6},
    {"SIP Digest", 10},
    {"HTTP_DIGEST_MD5", 15},
    {"NASS-Bundled", 12},
    {0, 0}
};

/**
 * Convert the SIP Algorithm to its type
 * @param algorithm - the SIP Algorithm
 * @returns the algorithm type
 */
unsigned char get_algorithm_type(str algorithm) {
    int i;
    for (i = 0; algorithm_types[i].len > 0; i++)
        if (algorithm_types[i].len == algorithm.len
                && strncasecmp(algorithm_types[i].s, algorithm.s, algorithm.len)
                == 0)
            return i;
    return AUTH_UNKNOWN;
}

/**
 * Convert the Diameter Authorization Scheme to its type
 * @param scheme - the Diameter Authorization Scheme
 * @returns the SIP Algorithm
 */
unsigned char get_auth_scheme_type(str scheme) {
    int i;
    for (i = 0; auth_scheme_types[i].len > 0; i++)
        if (auth_scheme_types[i].len == scheme.len &&
                strncasecmp(auth_scheme_types[i].s, scheme.s, scheme.len) == 0)
            return i;
    return AUTH_UNKNOWN;
}

static inline int get_ha1(struct username* _username, str* _domain,
        const str* _table, char* _ha1, db1_res_t** res) {

    return 0;
}

/*
 * Authorize digest credentials
 */
static int digest_authenticate(struct sip_msg* msg, str *realm,
        str *table, hdr_types_t hftype) {
    return 0;
}

/**
 * Starts the reg_await_timer for an authentication vector.
 * @param av - the authentication vector
 */
inline void start_reg_await_timer(auth_vector *av) {
    av->expires = get_ticks() + auth_vector_timeout;
    av->status = AUTH_VECTOR_SENT;
}

/**
 * Timer callback for reg await timers.
 * Drops the auth vectors that have been sent and are expired
 * Also drops the useless auth vectors - used and no longer needed
 * @param ticks - what's the time
 * @param param - a given parameter to be called with
 */
void reg_await_timer(unsigned int ticks, void* param) {
    auth_userdata *aud, *aud_next;
    auth_vector *av, *av_next;
    int i;

    LM_DBG("Looking for expired/useless at %d\n", ticks);
    for (i = 0; i < act_auth_data_hash_size; i++) {
        auth_data_lock(i);
        aud = auth_data[i].head;
        while (aud) {
            LM_DBG("Slot %4d <%.*s>\n",
                    aud->hash, aud->private_identity.len, aud->private_identity.s);
            aud_next = aud->next;
            av = aud->head;
            while (av) {
                LM_DBG(".. AV %4d - %d Exp %3d  %p\n",
                        av->item_number, av->status, (int) av->expires, av);
                av_next = av->next;
                if (av->status == AUTH_VECTOR_USELESS ||
                        ((av->status == AUTH_VECTOR_USED || av->status == AUTH_VECTOR_SENT) && av->expires < ticks)
                        ) {
                    LM_DBG("... dropping av %d - %d\n",
                            av->item_number, av->status);
                    if (av->prev) av->prev->next = av->next;
                    else aud->head = av->next;
                    if (av->next) av->next->prev = av->prev;
                    else aud->tail = av->prev;
                    free_auth_vector(av);
                }
                av = av_next;
            }
            if (!aud->head) {
                if (aud->expires == 0) {
                    LM_DBG("... started empty aud drop timer\n");
                    aud->expires = ticks + auth_data_timeout;
                } else
                    if (aud->expires < ticks) {
                    LM_DBG("... dropping aud \n");
                    if (aud->prev) aud->prev->next = aud->next;
                    else auth_data[i].head = aud->next;
                    if (aud->next) aud->next->prev = aud->prev;
                    else auth_data[i].tail = aud->prev;
                    free_auth_userdata(aud);
                }
            } else aud->expires = 0;

            aud = aud_next;
        }
        auth_data_unlock(i);
    }
    LM_DBG("[DONE] Looking for expired/useless at %d\n", ticks);
}

/*
 * Authenticate using Proxy-Authorize header field
 */

/*
int proxy_authenticate(struct sip_msg* _m, char* _realm, char* _table) {
    str srealm;
    str stable;

    if (_table == NULL) {
        LM_ERR("invalid table parameter\n");
        return -1;
    }

    stable.s = _table;
    stable.len = strlen(stable.s);

    if (get_str_fparam(&srealm, _m, (fparam_t*) _realm) < 0) {
        LM_ERR("failed to get realm value\n");
        return -1; //AUTH_ERROR;
    }

    if (srealm.len == 0) {
        LM_ERR("invalid realm parameter - empty value\n");
        return -1; //AUTH_ERROR;
    }
    LM_DBG("realm value [%.*s]\n", srealm.len, srealm.s);

    return digest_authenticate(_m, &srealm, &stable, HDR_PROXYAUTH_T);
}
 */
int challenge(struct sip_msg* msg, char* str1, char* alg, int is_proxy_auth, char *route) {

    str realm = {0, 0}, algo = {0,0};
    unsigned int aud_hash;
    str private_identity, public_identity, auts = {0, 0}, nonce = {0, 0};
    auth_vector *av = 0;
    int algo_type = 0;
    str route_name;

    saved_transaction_t* saved_t;
    tm_cell_t *t = 0;
    cfg_action_t* cfg_action;

    if (fixup_get_svalue(msg, (gparam_t*) route, &route_name) != 0) {
        LM_ERR("no async route block for assign_server_unreg\n");
        return -1;
    }
    
    if (!alg) {
	LM_DBG("no algorithm specified in cfg... using default\n");
    } else {
	if (get_str_fparam(&algo, msg, (fparam_t*) alg) < 0) {
	    LM_ERR("failed to get auth algorithm\n");
	    return -1;
	}
    }
    
    LM_DBG("Looking for route block [%.*s]\n", route_name.len, route_name.s);
    int ri = route_get(&main_rt, route_name.s);
    if (ri < 0) {
        LM_ERR("unable to find route block [%.*s]\n", route_name.len, route_name.s);
        return -1;
    }
    cfg_action = main_rt.rlist[ri];
    if (cfg_action == NULL) {
        LM_ERR("empty action lists in route block [%.*s]\n", route_name.len, route_name.s);
        return -1;
    }

    if (get_str_fparam(&realm, msg, (fparam_t*) str1) < 0) {
        LM_ERR("failed to get realm value\n");
        return CSCF_RETURN_ERROR;
    }

    if (realm.len == 0) {
        LM_ERR("invalid realm value - empty content\n");
        return CSCF_RETURN_ERROR;
    }

    create_return_code(CSCF_RETURN_ERROR);

    LM_DBG("Need to challenge for realm [%.*s]\n", realm.len, realm.s);

    if (msg->first_line.type != SIP_REQUEST) {
        LM_ERR("This message is not a request\n");
        return CSCF_RETURN_ERROR;
    }
    if (!is_proxy_auth) {
        LM_DBG("Checking if REGISTER is authorized for realm [%.*s]...\n", realm.len, realm.s);

        /* First check the parameters */
        if (msg->first_line.u.request.method.len != 8 ||
                memcmp(msg->first_line.u.request.method.s, "REGISTER", 8) != 0) {
            LM_ERR("This message is not a REGISTER request\n");
            return CSCF_RETURN_ERROR;
        }
    }

    /* get the private_identity */
    private_identity = cscf_get_private_identity(msg, realm);
    if (!private_identity.len) {
        LM_ERR("No private identity specified (Authorization: username)\n");
        stateful_request_reply(msg, 403, MSG_403_NO_PRIVATE);
        return CSCF_RETURN_BREAK;
    }
    /* get the public_identity */
    public_identity = cscf_get_public_identity(msg);
    if (!public_identity.len) {
        LM_ERR("No public identity specified (To:)\n");
        stateful_request_reply(msg, 403, MSG_403_NO_PUBLIC);
        return CSCF_RETURN_BREAK;
    }

    if (algo.len > 0) {
	algo_type = get_algorithm_type(algo);
    } else {
	algo_type = registration_default_algorithm_type;
    }
    
//    /* check if it is a synchronization request */
//    //TODO this is MAR syncing - have removed it currently - TOD maybe put back in
//    auts = ims_get_auts(msg, realm, is_proxy_auth);
//    if (auts.len) {
//        LM_DBG("IMS Auth Synchronization requested <%.*s>\n", auts.len, auts.s);
//
//        nonce = ims_get_nonce(msg, realm);
//        if (nonce.len == 0) {
//            LM_DBG("Nonce not found (Authorization: nonce)\n");
//            stateful_request_reply(msg, 403, MSG_403_NO_NONCE);
//            return CSCF_RETURN_BREAK;
//        }
//        av = get_auth_vector(private_identity, public_identity, AUTH_VECTOR_USED, &nonce, &aud_hash);
//        if (!av)
//            av = get_auth_vector(private_identity, public_identity, AUTH_VECTOR_SENT, &nonce, &aud_hash);
//
//        if (!av) {
//            LM_ERR("nonce not recognized as sent, no sync!\n");
//            auts.len = 0;
//            auts.s = 0;
//        } else {
//            av->status = AUTH_VECTOR_USELESS;
//            auth_data_unlock(aud_hash);
//            av = 0;
//            resync = 1;
//        }
//    }

    //RICHARD changed this
    //Previous approach sent MAR, got MAA then put auth vectors into queue
    //Then we try and get that auth vector out the queue (it might be used by someone else so we loop)
    //new approach
    //we do MAR get MAA (asynchronously) get auth vector use it to pack the vector etc.
    //set it to sent and set an expires on it
    //then add it to the queue!

    /* loop because some other process might steal the auth_vector that we just retrieved */
    //while (!(av = get_auth_vector(private_identity, public_identity, AUTH_VECTOR_UNUSED, 0, &aud_hash))) {

    if ((av = get_auth_vector(private_identity, public_identity, AUTH_VECTOR_UNUSED, 0, &aud_hash))) {
        if (!av) {
            LM_ERR("Error retrieving an auth vector\n");
            return CSCF_RETURN_ERROR;
        }

        if (!pack_challenge(msg, realm, av, is_proxy_auth)) {
            stateful_request_reply(msg, 500, MSG_500_PACK_AV);
            auth_data_unlock(aud_hash);
            return CSCF_RETURN_ERROR;
        }

        start_reg_await_timer(av); //start the timer to remove stale or unused Auth Vectors
        if (is_proxy_auth) {
            stateful_request_reply(msg, 407, MSG_407_CHALLENGE);
        } else {
            stateful_request_reply(msg, 401, MSG_401_CHALLENGE);
        }
        auth_data_unlock(aud_hash);

    } else {

        //before we send lets suspend the transaction
        t = tmb.t_gett();
        if (t == NULL || t == T_UNDEFINED) {
            if (tmb.t_newtran(msg) < 0) {
                LM_ERR("cannot create the transaction for MAR async\n");
                stateful_request_reply(msg, 480, MSG_480_DIAMETER_ERROR);
                return CSCF_RETURN_BREAK;
            }
            t = tmb.t_gett();
            if (t == NULL || t == T_UNDEFINED) {
                LM_ERR("cannot lookup the transaction\n");
                stateful_request_reply(msg, 480, MSG_480_DIAMETER_ERROR);
                return CSCF_RETURN_BREAK;
            }
        }

        saved_t = shm_malloc(sizeof (saved_transaction_t));
        if (!saved_t) {
            LM_ERR("no more memory trying to save transaction state\n");
            return CSCF_RETURN_ERROR;

        }
        memset(saved_t, 0, sizeof (saved_transaction_t));
        saved_t->act = cfg_action;

        saved_t->realm.s = (char*) shm_malloc(realm.len + 1);
        if (!saved_t->realm.s) {
            LM_ERR("no more memory trying to save transaction state : callid\n");
            shm_free(saved_t);
            return CSCF_RETURN_ERROR;
        }
        memset(saved_t->realm.s, 0, realm.len + 1);
        memcpy(saved_t->realm.s, realm.s, realm.len);
        saved_t->realm.len = realm.len;

        saved_t->is_proxy_auth = is_proxy_auth;

        LM_DBG("Suspending SIP TM transaction\n");
        if (tmb.t_suspend(msg, &saved_t->tindex, &saved_t->tlabel) < 0) {
            LM_ERR("failed to suspend the TM processing\n");
            free_saved_transaction_data(saved_t);

            stateful_request_reply(msg, 480, MSG_480_DIAMETER_ERROR);
            return CSCF_RETURN_BREAK;
        }

        if (multimedia_auth_request(msg, public_identity, private_identity, av_request_at_once,
                auth_scheme_types[algo_type], nonce, auts, scscf_name_str, saved_t)!=0) {
            LM_ERR("ERR:I_MAR: Error sending MAR or MAR time-out\n");
            tmb.t_cancel_suspend(saved_t->tindex, saved_t->tlabel);
            free_saved_transaction_data(saved_t);
            stateful_request_reply(msg, 480, MSG_480_DIAMETER_ERROR);
            return CSCF_RETURN_BREAK;
        }
    }
    return CSCF_RETURN_BREAK;
}
int www_challenge2(struct sip_msg* msg, char* _route, char* str1, char* str2) {
    return challenge(msg, str1, 0, 0, _route);
}

int www_challenge3(struct sip_msg* msg, char* _route, char* str1, char* str2) {
    return challenge(msg, str1, str2, 0, _route);
}

int www_resync_auth(struct sip_msg* msg, char* _route, char* str1, char* str2) {

    str realm = {0, 0};
    unsigned int aud_hash;
    str private_identity, public_identity, auts = {0, 0}, nonce = {0, 0};
    auth_vector *av = 0;
    int algo_type;
    int is_proxy_auth=0;
    str route_name;

    saved_transaction_t* saved_t;
    tm_cell_t *t = 0;
    cfg_action_t* cfg_action;

    if (fixup_get_svalue(msg, (gparam_t*) _route, &route_name) != 0) {
        LM_ERR("no async route block for assign_server_unreg\n");
        return -1;
    }

    LM_DBG("Looking for route block [%.*s]\n", route_name.len, route_name.s);
    int ri = route_get(&main_rt, route_name.s);
    if (ri < 0) {
        LM_ERR("unable to find route block [%.*s]\n", route_name.len, route_name.s);
        return -1;
    }
    cfg_action = main_rt.rlist[ri];
    if (cfg_action == NULL) {
        LM_ERR("empty action lists in route block [%.*s]\n", route_name.len, route_name.s);
        return -1;
    }

    if (get_str_fparam(&realm, msg, (fparam_t*) str1) < 0) {
        LM_ERR("failed to get realm value\n");
        return CSCF_RETURN_ERROR;
    }

    if (realm.len == 0) {
        LM_ERR("invalid realm value - empty content\n");
        return CSCF_RETURN_ERROR;
    }

    create_return_code(CSCF_RETURN_ERROR);

    if (msg->first_line.type != SIP_REQUEST) {
        LM_ERR("This message is not a request\n");
        return CSCF_RETURN_ERROR;
    }

    /* get the private_identity */
    private_identity = cscf_get_private_identity(msg, realm);
    if (!private_identity.len) {
        LM_ERR("No private identity specified (Authorization: username)\n");
        stateful_request_reply(msg, 403, MSG_403_NO_PRIVATE);
        return CSCF_RETURN_BREAK;
    }
    /* get the public_identity */
    public_identity = cscf_get_public_identity(msg);
    if (!public_identity.len) {
        LM_ERR("No public identity specified (To:)\n");
        stateful_request_reply(msg, 403, MSG_403_NO_PUBLIC);
        return CSCF_RETURN_BREAK;
    }

    algo_type = registration_default_algorithm_type;

    /* check if it is a synchronization request */
    //TODO this is MAR syncing - have removed it currently - TOD maybe put back in
    auts = ims_get_auts(msg, realm, is_proxy_auth);
    if (auts.len) {
        LM_DBG("IMS Auth Synchronization requested <%.*s>\n", auts.len, auts.s);

        nonce = ims_get_nonce(msg, realm);
        if (nonce.len == 0) {
            LM_DBG("Nonce not found (Authorization: nonce)\n");
            stateful_request_reply(msg, 403, MSG_403_NO_NONCE);
            return CSCF_RETURN_BREAK;
        }
        av = get_auth_vector(private_identity, public_identity, AUTH_VECTOR_USED, &nonce, &aud_hash);
        if (!av)
            av = get_auth_vector(private_identity, public_identity, AUTH_VECTOR_SENT, &nonce, &aud_hash);

        if (!av) {
            LM_ERR("nonce not recognized as sent, no sync!\n");
            auts.len = 0;
            auts.s = 0;
        } else {
            av->status = AUTH_VECTOR_USELESS;
            auth_data_unlock(aud_hash);
            av = 0;
        }
    }

	//before we send lets suspend the transaction
	t = tmb.t_gett();
	if (t == NULL || t == T_UNDEFINED) {
		if (tmb.t_newtran(msg) < 0) {
			LM_ERR("cannot create the transaction for MAR async\n");
			stateful_request_reply(msg, 480, MSG_480_DIAMETER_ERROR);
			return CSCF_RETURN_BREAK;
		}
		t = tmb.t_gett();
		if (t == NULL || t == T_UNDEFINED) {
			LM_ERR("cannot lookup the transaction\n");
			stateful_request_reply(msg, 480, MSG_480_DIAMETER_ERROR);
			return CSCF_RETURN_BREAK;
		}
	}

	saved_t = shm_malloc(sizeof(saved_transaction_t));
	if (!saved_t) {
		LM_ERR("no more memory trying to save transaction state\n");
		return CSCF_RETURN_ERROR;

	}
	memset(saved_t, 0, sizeof(saved_transaction_t));
	saved_t->act = cfg_action;

	saved_t->realm.s = (char*) shm_malloc(realm.len + 1);
	if (!saved_t->realm.s) {
		LM_ERR("no more memory trying to save transaction state : callid\n");
		shm_free(saved_t);
		return CSCF_RETURN_ERROR;
	}
	memset(saved_t->realm.s, 0, realm.len + 1);
	memcpy(saved_t->realm.s, realm.s, realm.len);
	saved_t->realm.len = realm.len;

	saved_t->is_proxy_auth = is_proxy_auth;
	saved_t->is_resync = 1;

	LM_DBG("Suspending SIP TM transaction\n");
	if (tmb.t_suspend(msg, &saved_t->tindex, &saved_t->tlabel) < 0) {
		LM_ERR("failed to suspend the TM processing\n");
		free_saved_transaction_data(saved_t);

		stateful_request_reply(msg, 480, MSG_480_DIAMETER_ERROR);
		return CSCF_RETURN_BREAK;
	}

	if (multimedia_auth_request(msg, public_identity, private_identity,
			av_request_at_sync, auth_scheme_types[algo_type], nonce, auts,
			scscf_name_str, saved_t) != 0) {
		LM_ERR("ERR:I_MAR: Error sending MAR or MAR time-out\n");
		tmb.t_cancel_suspend(saved_t->tindex, saved_t->tlabel);
		free_saved_transaction_data(saved_t);
		stateful_request_reply(msg, 480, MSG_480_DIAMETER_ERROR);
		return CSCF_RETURN_BREAK;
	}

	return CSCF_RETURN_BREAK;
}

int proxy_challenge(struct sip_msg* msg, char* _route, char* str1, char* str2) {
    return challenge(msg, str1, str2, 1, _route);
}

/**
 * Replies to a REGISTER and also adds the need headers
 * Path and Service-Route are added.
 * @param msg - the SIP message to operator on
 * @param code - Reason Code for the response
 * @param text - Reason Phrase for the response
 * @returns #CSCF_RETURN_TRUE on success or #CSCF_RETURN_FALSE if not added
 */
int stateful_request_reply(struct sip_msg *msg, int code, char *text) {
    unsigned int hash, label;
    struct hdr_field *h;
    str t = {0, 0};
    if (parse_headers(msg, HDR_EOH_F, 0) < 0) {
        LM_ERR("Error parsing headers\n");
        return -1;
    }
    h = msg->headers;
    while (h) {
        if (h->name.len == 4 &&
                strncasecmp(h->name.s, "Path", 4) == 0) {
            t.s = h->name.s;
            t.len = h->len;
            ims_add_header_rpl(msg, &(t));
        }
        h = h->next;
    }

    /*if (code==200){
            ims_add_header_rpl(msg,&scscf_service_route);
    }*/ //TODO: need to get the service route from somewhere - registrar?


    if (tmb.t_get_trans_ident(msg, &hash, &label) < 0) {
        if (tmb.t_newtran(msg) < 0)
            LM_INFO("Failed creating SIP transaction\n");
    }
    return tmb.t_reply(msg, code, text);

}

/**
 * Replies to a REGISTER and also adds the need headers
 * Path and Service-Route are added.
 * @param msg - the SIP message to operator on
 * @param code - Reason Code for the response
 * @param text - Reason Phrase for the response
 * @returns #CSCF_RETURN_TRUE on success or #CSCF_RETURN_FALSE if not added
 */

int stateful_request_reply_async(struct cell* t_cell, struct sip_msg *msg, int code, char *text) {
    struct hdr_field *h;
    str t = {0, 0};
    if (parse_headers(msg, HDR_EOH_F, 0) < 0) {
        LM_ERR("Error parsing headers\n");
        return -1;
    }
    h = msg->headers;
    while (h) {
        if (h->name.len == 4 &&
                strncasecmp(h->name.s, "Path", 4) == 0) {
            t.s = h->name.s;
            t.len = h->len;
            ims_add_header_rpl(msg, &(t));
        }
        h = h->next;
    }

    return tmb.t_reply_trans(t_cell, msg, code, text);

}

int authenticate(struct sip_msg* msg, char* _realm, char* str2, int is_proxy_auth) {
    int ret = -1; //CSCF_RETURN_FALSE;
    unsigned int aud_hash = 0;
    str realm;
    str private_identity, public_identity;
    str nonce, response16, nc, cnonce, qop_str = {0, 0}, auts = {0, 0}, body, *next_nonce = &empty_s;
    enum qop_type qop = QOP_UNSPEC;
    str uri = {0, 0};
    HASHHEX expected, ha1, hbody, rspauth;
    int expected_len = 32;
    int expires = 0;
    auth_vector *av = 0;
    uint32_t nc_parsed = 0; /* the numerical representation of nc */

    ret = AUTH_ERROR;

    if (get_str_fparam(&realm, msg, (fparam_t*) _realm) < 0) {
        LM_ERR("failed to get realm value\n");
        return AUTH_NO_CREDENTIALS;
    }

    if (realm.len == 0) {
        LM_ERR("invalid realm value - empty content\n");
        return AUTH_NO_CREDENTIALS;
    }

    if (msg->first_line.type != SIP_REQUEST) {
        LM_ERR("This message is not a request\n");
        ret = AUTH_ERROR;
        goto end;
    }
    if (!is_proxy_auth) {
        LM_DBG("Checking if REGISTER is authorized for realm [%.*s]...\n", realm.len, realm.s);

        /* First check the parameters */
        if (msg->first_line.u.request.method.len != 8 ||
                memcmp(msg->first_line.u.request.method.s, "REGISTER", 8) != 0) {
            LM_ERR("This message is not a REGISTER request\n");
            ret = AUTH_ERROR;
            goto end;
        }
    }

    if (!realm.len) {
        LM_ERR("No realm found\n");
        return 0; //CSCF_RETURN_BREAK;
    }

    private_identity = cscf_get_private_identity(msg, realm);
    if (!private_identity.len) {
        LM_ERR("private identity missing\n");
        return AUTH_NO_CREDENTIALS;
    }

    public_identity = cscf_get_public_identity(msg);
    if (!public_identity.len) {
        LM_ERR("public identity missing\n");
        return AUTH_NO_CREDENTIALS;
    }

    if (!get_nonce_response(msg, realm, &nonce, &response16, &qop, &qop_str, &nc, &cnonce, &uri, is_proxy_auth) ||
            !nonce.len || !response16.len) {
        LM_DBG("Nonce or response missing: nonce len [%i], response16 len[%i]\n", nonce.len, response16.len);
        return AUTH_ERROR;
    }

    if (qop == QOP_AUTHINT) {
        body = ims_get_body(msg);
        calc_H(&body, hbody);
    }

    /* first, look for an already used vector (if nonce reuse is enabled) */
    if (max_nonce_reuse > 0) {
        LM_DBG("look for an already used vector for %.*s\n",
                private_identity.len, private_identity.s);
        av = get_auth_vector(private_identity, public_identity, AUTH_VECTOR_USED, &nonce, &aud_hash);
    }

    if (!av) {
        /* if none found, or nonce reuse is disabled, look for a fresh vector
         * We should also drop every other used vector at this point
         * (there souldn't be more than one) */
    	LM_DBG("Looking for auth vector based on IMPI: [%.*s] and IMPU: [%.*s]\n", private_identity.len, private_identity.s, public_identity.len, public_identity.s);
        auth_userdata *aud;
        auth_vector *av_it;
        aud = get_auth_userdata(private_identity, public_identity);
        if (aud) {
            av_it = aud->head;
            while (av_it) {
                if (av_it->status == AUTH_VECTOR_USED) {
                    LM_DBG("vector %p is marked for deletion\n", av_it);
                    av_it->status = AUTH_VECTOR_USELESS;
                }
                av_it = av_it->next;
            }
            auth_data_unlock(aud->hash);
        }

        LM_DBG("look for a fresh vector for %.*s\n",
                private_identity.len, private_identity.s);
        av = get_auth_vector(private_identity, public_identity, AUTH_VECTOR_SENT, &nonce, &aud_hash);
    }

    LM_INFO("uri=%.*s nonce=%.*s response=%.*s qop=%.*s nc=%.*s cnonce=%.*s hbody=%.*s\n",
            uri.len, uri.s,
            nonce.len, nonce.s,
            response16.len, response16.s,
            qop_str.len, qop_str.s,
            nc.len, nc.s,
            cnonce.len, cnonce.s,
            32, hbody);

    if (!av) {
        LM_DBG("no matching auth vector found - maybe timer expired\n");

        if (ignore_failed_auth) {
            LM_WARN("NB: Ignoring all failed auth - check your config if you don't expect this\n");
            ret = AUTH_OK;
        }

        goto end;
    }

    if (qop != QOP_UNSPEC) {
        /* if QOP is sent, nc must be specified */
        /* the expected nc is the last used one plus 1 */
        int p;
        for (p = 0; p < 8; ++p) { /* nc is 8LHEX (RFC 2617 §3.2.2) */
            nc_parsed = (nc_parsed << 4) | UNHEX((int) nc.s[p]);
        }
        LM_DBG("nc is %08x, expected: %08x\n",
                nc_parsed, av->use_nb + 1);
        if (nc_parsed <= av->use_nb) { /* nc is lower than expected */
            ret = AUTH_NONCE_REUSED;
            av->status = AUTH_VECTOR_USELESS; /* invalidate this vector if any mistake/error occurs */
            goto cleanup;
        } else if (nc_parsed > av->use_nb + 1) { /* nc is bigger than expected */
            ret = AUTH_ERROR;
            av->status = AUTH_VECTOR_USELESS;
            goto cleanup;
        }
    }

    switch (av->type) {
        case AUTH_AKAV1_MD5:
        case AUTH_AKAV2_MD5:
        case AUTH_MD5:
            calc_HA1(HA_MD5, &private_identity, &realm, &(av->authorization), &(av->authenticate), &cnonce, ha1);
            calc_response(ha1, &(av->authenticate),
                    &nc,
                    &cnonce,
                    &qop_str,
                    qop == QOP_AUTHINT,
                    &msg->first_line.u.request.method, &uri, hbody, expected);
            LM_INFO("UE said: %.*s and we expect %.*s ha1 %.*s (%.*s)\n",
                    response16.len, response16.s, 
                    /*av->authorization.len,av->authorization.s,*/32, expected,
                    32, ha1,
                    msg->first_line.u.request.method.len, msg->first_line.u.request.method.s);
            break;
        case AUTH_SIP_DIGEST:
        case AUTH_DIGEST:
            // memcpy of received HA1
            memcpy(ha1, av->authorization.s, HASHHEXLEN); 
            calc_response(ha1, &(av->authenticate),
                    &nc,
                    &cnonce,
                    &qop_str,
                    qop == QOP_AUTHINT,
                    &msg->first_line.u.request.method, &uri, hbody, expected);
            LM_INFO("UE said: %.*s and we expect %.*s ha1 %.*s (%.*s)\n",
                    response16.len, response16.s, 
                    32,expected, 
                    32,ha1, 
                    msg->first_line.u.request.method.len, msg->first_line.u.request.method.s);
            break;
        default:
            LM_ERR("algorithm %.*s is not handled.\n",
                    algorithm_types[av->type].len, algorithm_types[av->type].s);
            ret = AUTH_ERROR;
            goto cleanup; /* release aud before returning */
    }

    expires = cscf_get_max_expires(msg, 0);

    if (response16.len == expected_len && strncasecmp(response16.s, expected, response16.len) == 0) {
        if (max_nonce_reuse > 0 && av->status == AUTH_VECTOR_SENT) {
            /* first use of a reusable vector */
            /* set the vector's new timeout */
            LM_DBG("vector %p now expires in %d seconds\n", av, auth_used_vector_timeout);
            av->expires = get_ticks() + auth_used_vector_timeout;
        }
        av->use_nb++;
        LM_DBG("vector %p successfully used %d time(s)\n", av, av->use_nb);

        if (av->use_nb == max_nonce_reuse + 1) {
            LM_DBG("vector %p isn't fresh anymore, recycle it with a new nonce\n", av);

            int i;
            char y[NONCE_LEN];
            for (i = 0; i < NONCE_LEN; i++)
                y[i] = (unsigned char) ((int) (256.0 * rand() / (RAND_MAX + 1.0)));

            if (unlikely((av->authenticate.len < 2 * NONCE_LEN))) {
                if (av->authenticate.s) {
                    shm_free(av->authenticate.s);
                }
                av->authenticate.len = 2 * NONCE_LEN;
                av->authenticate.s = shm_malloc(av->authenticate.len);
            }

            if (!av->authenticate.s) {
                LM_ERR("new_auth_vector: failed allocating %d bytes!\n", av->authenticate.len);
                av->authenticate.len = 0;
                goto cleanup;
            }

            av->authenticate.len = bin_to_base16(y, NONCE_LEN, av->authenticate.s);

            next_nonce = &(av->authenticate);
            av->status = AUTH_VECTOR_USED;
            av->use_nb = 0;
            av->expires = get_ticks() + auth_used_vector_timeout; /* reset the timer */

        } else if (expires == 0) { /* de-registration */
            LM_DBG("de-registration, vector %p isn't needed anymore\n", av);
            av->status = AUTH_VECTOR_USELESS;
        } else {
            av->status = AUTH_VECTOR_USED;
            /* nextnonce is the current nonce */
            next_nonce = &nonce;
        }
        ret = AUTH_OK;

        if (add_authinfo_hdr && expires != 0 /* don't add auth. info if de-registation */) {
            /* calculate rspauth */
            calc_response(ha1, &nonce,
                    &nc,
                    &cnonce,
                    &qop_str,
                    qop == QOP_AUTHINT,
                    0, &uri, hbody, rspauth);

            add_authinfo_resp_hdr(msg, *next_nonce, qop_str, rspauth, cnonce, nc);
        }


    } else {
    	char authorise[200];
    	char authenticate_bin[200];
    	char authenticate_hex[200];
    	memset(authorise, 0, 200);
    	memset(authenticate_bin, 0, 200);
    	memset(authenticate_hex, 0, 200);

    	int authorise_len = bin_to_base16(av->authorization.s, av->authorization.len, authorise);
    	int authenticate_len = base64_to_bin(av->authenticate.s, av->authenticate.len, authenticate_bin);
    	int authenticate_hex_len = bin_to_base16(authenticate_bin, authenticate_len, authenticate_hex);
    	av->status = AUTH_VECTOR_USELESS; /* first mistake, you're out! (but maybe it's synchronization) */
        LM_DBG("UE said: %.*s, but we expect %.*s : authenticate(b64) is [%.*s], authenticate(hex) is [%.*s], authorise is [%d] [%.*s]\n",
                response16.len, response16.s,
                32, expected,
                av->authenticate.len, av->authenticate.s,
                authenticate_hex_len,authenticate_hex,
                authorise_len,
                authorise_len, authorise);
//        /* check for auts in authorization header - if it is then we need to resync */
		auts = ims_get_auts(msg, realm, is_proxy_auth);
		if (auts.len) {
			LM_DBG("IMS Auth Synchronization requested <%.*s>\n", auts.len, auts.s);
			ret = AUTH_RESYNC_REQUESTED;
			av->status = AUTH_VECTOR_SENT;
		} else {
			ret = AUTH_INVALID_PASSWORD;
		}
    }

    if (ignore_failed_auth) {
        LM_WARN("NB: Ignoring all failed auth - check your config if you don't expect this\n");
        ret = AUTH_OK;
    }

cleanup:
    auth_data_unlock(aud_hash);
end:
    return ret;
}

/*
 * Authenticate using WWW-Authorize header field
 */
int www_authenticate(struct sip_msg* msg, char* _realm, char* str2) {
    return authenticate(msg, _realm, str2, 0);
}

/*
 * Authenticate using WWW-Authorize header field
 */
int proxy_authenticate(struct sip_msg* msg, char* _realm, char* str2) {
    return authenticate(msg, _realm, str2, 1);
}

/**
 * @brief bind functions to IMS AUTH API structure
 */
int bind_ims_auth(ims_auth_api_t * api) {
    if (!api) {
        ERR("Invalid parameter value\n");
        return -1;
    }
    api->digest_authenticate = digest_authenticate;

    return 0;
}

/**
 * Retrieve an authentication vector.
 * \note returns with a lock, so unlock it when done
 * @param private_identity - the private identity
 * @param public_identity - the public identity
 * @param status - the status of the authentication vector
 * @param nonce - the nonce in the auth vector
 * @param hash - the hash to unlock when done
 * @returns the auth_vector* if found or NULL if not
 */
auth_vector * get_auth_vector(str private_identity, str public_identity, int status, str *nonce, unsigned int *hash) {
    auth_userdata *aud;
    auth_vector *av;
    aud = get_auth_userdata(private_identity, public_identity);
    if (!aud) {
        LM_ERR("no auth userdata\n");
        goto error;
    }

    av = aud->head;
    while (av) {
        LM_DBG("looping through AV status is %d and were looking for %d\n", av->status, status);
        if (av->status == status && (nonce == 0 || (nonce->len == av->authenticate.len && memcmp(nonce->s, av->authenticate.s, nonce->len) == 0))) {
            LM_DBG("Found result\n");
            *hash = aud->hash;
            return av;
        }
        av = av->next;
    }

error:
    if (aud) auth_data_unlock(aud->hash);
    return 0;
}

/**
 * Locks the required slot of the auth_data.
 * @param hash - the index of the slot
 */
inline void auth_data_lock(unsigned int hash) {
    lock_get(auth_data[(hash)].lock);
}

/**
 * UnLocks the required slot of the auth_data
 * @param hash - the index of the slot
 */
inline void auth_data_unlock(unsigned int hash) {
    lock_release(auth_data[(hash)].lock);
}

/**
 * Initializes the Authorization Data structures.
 * @param size - size of the hash table
 * @returns 1 on success or 0 on error
 */
int auth_data_init(int size) {
    int i;
    auth_data = shm_malloc(sizeof (auth_hash_slot_t) * size);
    if (!auth_data) {
        LM_ERR("error allocating mem\n");
        return 0;
    }
    memset(auth_data, 0, sizeof (auth_hash_slot_t) * size);
    for (i = 0; i < size; i++) {
        auth_data[i].lock = lock_alloc();
        lock_init(auth_data[i].lock);
    }
    act_auth_data_hash_size = size;
    return 1;
}

/**
 * Destroy the Authorization Data structures */
void auth_data_destroy() {
    int i;
    auth_userdata *aud, *next;
    for (i = 0; i < act_auth_data_hash_size; i++) {
        auth_data_lock(i);
        lock_destroy(auth_data[i].lock);
        lock_dealloc(auth_data[i].lock);
        aud = auth_data[i].head;
        while (aud) {
            next = aud->next;
            free_auth_userdata(aud);
            aud = next;
        }
    }
    if (auth_data) shm_free(auth_data);
}

/**
 * Create new authorization vector
 * @param item_number - number to index it in the vectors list
 * @param auth_scheme - Diameter Authorization Scheme
 * @param authenticate - the challenge
 * @param authorization - the expected response
 * @param ck - the cypher key
 * @param ik - the integrity key
 * @returns the new auth_vector* or NULL on error
 */
auth_vector * new_auth_vector(int item_number, str auth_scheme, str authenticate,
        str authorization, str ck, str ik) {
    auth_vector *x = 0;
    x = shm_malloc(sizeof (auth_vector));
    if (!x) {
        LM_ERR("error allocating mem\n");
        goto done;
    }
    memset(x, 0, sizeof (auth_vector));
    x->item_number = item_number;
    x->type = get_auth_scheme_type(auth_scheme);
    switch (x->type) {
        case AUTH_AKAV1_MD5:
        case AUTH_AKAV2_MD5:
            /* AKA */
            x->authenticate.len = authenticate.len * 4 / 3 + 4;
            x->authenticate.s = shm_malloc(x->authenticate.len);
            if (!x->authenticate.s) {
                LM_ERR("error allocating mem\n");
                goto done;
            }
            x->authenticate.len = bin_to_base64(authenticate.s, authenticate.len,
                    x->authenticate.s);

            x->authorization.len = authorization.len;
            x->authorization.s = shm_malloc(x->authorization.len);
            if (!x->authorization.s) {
                LM_ERR("error allocating mem\n");
                goto done;
            }
            memcpy(x->authorization.s, authorization.s, authorization.len);
            x->ck.len = ck.len;
            x->ck.s = shm_malloc(ck.len);
            if (!x->ck.s) {
                LM_ERR("error allocating mem\n");
                goto done;
            }
            memcpy(x->ck.s, ck.s, ck.len);

            x->ik.len = ik.len;
            x->ik.s = shm_malloc(ik.len);
            if (!x->ik.s) {
                LM_ERR("error allocating mem\n");
                goto done;
            }
            memcpy(x->ik.s, ik.s, ik.len);
            break;

        case AUTH_MD5:
            /* MD5 */
            x->authenticate.len = authenticate.len * 2;
            x->authenticate.s = shm_malloc(x->authenticate.len);
            if (!x->authenticate.s) {
                LM_ERR("new_auth_vector: error allocating mem\n");
                goto done;
            }
            x->authenticate.len = bin_to_base16(authenticate.s, authenticate.len,
                    x->authenticate.s);

            x->authorization.len = authorization.len;
            x->authorization.s = shm_malloc(x->authorization.len);
            if (!x->authorization.s) {
                LM_ERR("new_auth_vector: error allocating mem\n");
                goto done;
            }
            memcpy(x->authorization.s, authorization.s, authorization.len);
            x->authorization.len = authorization.len;
            break;
        case AUTH_DIGEST:
        case AUTH_SIP_DIGEST:
        {
            int i;
            char y[NONCE_LEN];
            for (i = 0; i < NONCE_LEN; i++)
                y[i] = (unsigned char) ((int) (256.0 * rand() / (RAND_MAX + 1.0)));
            x->authenticate.len = 2 * NONCE_LEN;
            x->authenticate.s = shm_malloc(x->authenticate.len);
            if (!x->authenticate.s) {
                LM_ERR("new_auth_vector: failed allocating %d bytes!\n", x->authenticate.len);
                x->authenticate.len = 0;
                goto done;
            }
            x->authenticate.len = bin_to_base16(y, NONCE_LEN, x->authenticate.s);
        }

            x->authorization.len = authorization.len;
            x->authorization.s = shm_malloc(x->authorization.len);
            if (!x->authorization.s) {
                LM_ERR("new_auth_vector: error allocating mem\n");
                x->authorization.len = 0;
                goto done;
            }
            memcpy(x->authorization.s, authorization.s, authorization.len);
            x->authorization.len = authorization.len;

            break;
        case AUTH_HTTP_DIGEST_MD5:
            x->authenticate.len = authenticate.len;
            x->authenticate.s = shm_malloc(x->authenticate.len);
            if (!x->authenticate.s) {
                LM_ERR("new_auth_vector: error allocating mem\n");
                x->authenticate.len = 0;
                goto done;
            }
            memcpy(x->authenticate.s, authenticate.s, authenticate.len);

            x->authorization.len = authorization.len;
            x->authorization.s = shm_malloc(x->authorization.len);
            if (!x->authorization.s) {
                LM_ERR("new_auth_vector: error allocating mem\n");
                x->authorization.len = 0;
                goto done;
            }
            memcpy(x->authorization.s, authorization.s, authorization.len);
            break;
        case AUTH_EARLY_IMS:
            /* early IMS */
            x->authenticate.len = 0;
            x->authenticate.s = 0;
            x->authorization.len = authorization.len;
            x->authorization.s = shm_malloc(x->authorization.len);
            if (!x->authorization.s) {
                LM_ERR("new_auth_vector: error allocating mem\n");
                goto done;
            }
            memcpy(x->authorization.s, authorization.s, authorization.len);
            x->authorization.len = authorization.len;
            break;
        case AUTH_NASS_BUNDLED:
            /* NASS-Bundled */
            x->authenticate.len = 0;
            x->authenticate.s = 0;
            x->authorization.len = authorization.len;
            x->authorization.s = shm_malloc(x->authorization.len);
            if (!x->authorization.s) {
                LM_ERR("new_auth_vector: error allocating mem\n");
                goto done;
            }
            memcpy(x->authorization.s, authorization.s, authorization.len);
            x->authorization.len = authorization.len;
            break;

        default:
            /* all else */
            x->authenticate.len = 0;
            x->authenticate.s = 0;

    }

    x->use_nb = 0;

    x->next = 0;
    x->prev = 0;
    x->status = AUTH_VECTOR_UNUSED;
    x->expires = 0;

    LM_DBG("new auth-vector with ck [%.*s] with status %d\n", x->ck.len, x->ck.s, x->status);

done:
    return x;
}

/**
 * Frees the memory taken by a authentication vector
 * @param av - the vector to be freed
 */
void free_auth_vector(auth_vector * av) {
    if (av) {
        if (av->authenticate.s) shm_free(av->authenticate.s);
        if (av->authorization.s) shm_free(av->authorization.s);
        if (av->ck.s) shm_free(av->ck.s);
        if (av->ik.s) shm_free(av->ik.s);
        shm_free(av);
    }
}

/**
 * Creates a new Authorization Userdata structure.
 * @param private_identity - the private identity to attach to
 * @param public_identity - the public identity to attach to
 * @returns the new auth_userdata* on success or NULL on error
 */
auth_userdata * new_auth_userdata(str private_identity, str public_identity) {
    auth_userdata *x = 0;

    x = shm_malloc(sizeof (auth_userdata));
    if (!x) {
        LM_ERR("error allocating mem\n");
        goto done;
    }

    x->private_identity.len = private_identity.len;
    x->private_identity.s = shm_malloc(private_identity.len);
    if (!x) {
        LM_ERR("error allocating mem\n");
        goto done;
    }
    memcpy(x->private_identity.s, private_identity.s, private_identity.len);

    x->public_identity.len = public_identity.len;
    x->public_identity.s = shm_malloc(public_identity.len);
    if (!x) {
        LM_ERR("error allocating mem\n");
        goto done;
    }
    memcpy(x->public_identity.s, public_identity.s, public_identity.len);

    x->head = 0;
    x->tail = 0;

    x->next = 0;
    x->prev = 0;

done:
    return x;
}

/**
 * Deallocates the auth_userdata.
 * @param aud - the auth_userdata to be deallocated
 */
void free_auth_userdata(auth_userdata * aud) {
    auth_vector *av, *next;
    if (aud) {
        if (aud->private_identity.s) shm_free(aud->private_identity.s);
        if (aud->public_identity.s) shm_free(aud->public_identity.s);
        av = aud->head;
        while (av) {
            next = av->next;
            free_auth_vector(av);
            av = next;
        }
        shm_free(aud);
    }
}

/**
 * Computes a hash based on the private and public identities
 * @param private_identity - the private identity
 * @param public_identity - the public identity
 * @returns the hash % Auth_data->size
 */
inline unsigned int get_hash_auth(str private_identity, str public_identity) {
if (av_check_only_impu)
	return core_hash(&public_identity, 0, act_auth_data_hash_size);
else
	return core_hash(&public_identity, 0, act_auth_data_hash_size);
/*


#define h_inc h+=v^(v>>3)
    char* p;
    register unsigned v;
    register unsigned h;

    h = 0;
    for (p = private_identity.s; p <= (private_identity.s + private_identity.len - 4); p += 4) {
        v = (*p << 24)+(p[1] << 16)+(p[2] << 8) + p[3];
        h_inc;
    }
    v = 0;
    for (; p < (private_identity.s + private_identity.len); p++) {
        v <<= 8;
        v += *p;
    }
    h_inc;
    for (p = public_identity.s; p <= (public_identity.s + public_identity.len - 4); p += 4) {
        v = (*p << 24)+(p[1] << 16)+(p[2] << 8) + p[3];
        h_inc;
    }
    v = 0;
    for (; p < (public_identity.s + public_identity.len); p++) {
        v <<= 8;
        v += *p;
    }

    h = ((h)+(h >> 11))+((h >> 13)+(h >> 23));
    return (h) % auth_data_hash_size;
#undef h_inc
*/
}

/**
 * Retrieve the auth_userdata for a user.
 * \note you will return with lock on the hash slot, so release it!
 * @param private_identity - the private identity
 * @param public_identity - the public identity
 * @returns the auth_userdata* found or newly created on success, NULL on error
 */
auth_userdata * get_auth_userdata(str private_identity, str public_identity) {

    unsigned int hash = 0;
    auth_userdata *aud = 0;

    hash = get_hash_auth(private_identity, public_identity);
    auth_data_lock(hash);
    aud = auth_data[hash].head;
    if (av_check_only_impu)
      LM_DBG("Searching auth_userdata for IMPU %.*s (Hash %d)\n", public_identity.len, public_identity.s, hash);
    else
      LM_DBG("Searching auth_userdata for IMPU %.*s / IMPI %.*s (Hash %d)\n", public_identity.len, public_identity.s,
        private_identity.len, private_identity.s, hash);

    while (aud) {
	if (av_check_only_impu) {
		if (aud->public_identity.len == public_identity.len &&
		        memcmp(aud->public_identity.s, public_identity.s, public_identity.len) == 0) {
                    LM_DBG("Found auth_userdata\n");
		    return aud;
		}
	} else {
		if (aud->private_identity.len == private_identity.len &&
		        aud->public_identity.len == public_identity.len &&
		        memcmp(aud->private_identity.s, private_identity.s, private_identity.len) == 0 &&
		        memcmp(aud->public_identity.s, public_identity.s, public_identity.len) == 0) {
                    LM_DBG("Found auth_userdata\n");
		    return aud;
		}
	}

        aud = aud->next;
    }
    /* if we get here, there is no auth_userdata for this user */
    aud = new_auth_userdata(private_identity, public_identity);
    if (!aud) {
        auth_data_unlock(hash);
        return 0;
    }

    aud->prev = auth_data[hash].tail;
    aud->next = 0;
    aud->hash = hash;

    if (!auth_data[hash].head) auth_data[hash].head = aud;
    if (auth_data[hash].tail) auth_data[hash].tail->next = aud;
    auth_data[hash].tail = aud;

    return aud;
}

/**
 * Sends a Multimedia-Authentication-Response to retrieve some authentication vectors and maybe synchronize.
 * Must respond with a SIP reply every time it returns 0
 * @param msg - the SIP REGISTER message
 * @param public_identity - the public identity
 * @param private_identity - the private identity
 * @param count - how many vectors to request
 * @param algorithm - which algorithm to request
 * @param nonce - the challenge that will be sent
 * @param auts - the AKA synchronization or empty string if not a synchronization
 * @param server_name - the S-CSCF name to be saved on the HSS
 * @returns 1 on success, 0 on failure
 */
int multimedia_auth_request(struct sip_msg *msg, str public_identity, str private_identity,
        int count, str auth_scheme, str nonce, str auts, str servername, saved_transaction_t* transaction_data) {


    str authorization = {0, 0};
    int result = -1;

    int is_sync = 0;
    if (auts.len) {
        authorization.s = pkg_malloc(nonce.len * 3 / 4 + auts.len * 3 / 4 + 8);
        if (!authorization.s)  {
        	LM_ERR("no more pkg mem\n");
        	return result;
        }
        authorization.len = base64_to_bin(nonce.s, nonce.len, authorization.s);
        authorization.len = RAND_LEN;
        authorization.len += base64_to_bin(auts.s, auts.len, authorization.s + authorization.len);
        is_sync = 1;
    }

    if (is_sync) {
    	drop_auth_userdata(private_identity, public_identity);
    }


    LM_DBG("Sending MAR\n");
    result = cxdx_send_mar(msg, public_identity, private_identity, count, auth_scheme, authorization, servername, transaction_data);
    if (authorization.s) pkg_free(authorization.s);

    return result;
}

/**
 * Adds the WWW-Authenticate header for challenge, based on the authentication vector.
 * @param msg - SIP message to add the header to
 * @param realm - the realm
 * @param av - the authentication vector
 * @returns 1 on success, 0 on error
 */
int pack_challenge(struct sip_msg *msg, str realm, auth_vector *av, int is_proxy_auth) {
    str x = {0, 0};
    char ck[32], ik[32];
    int ck_len, ik_len;
    str *auth_prefix = is_proxy_auth ? &S_Proxy : &S_WWW;
    switch (av->type) {
        case AUTH_AKAV1_MD5:
        case AUTH_AKAV2_MD5:
            /* AKA */
            ck_len = bin_to_base16(av->ck.s, 16, ck);
            ik_len = bin_to_base16(av->ik.s, 16, ik);
            x.len = S_Authorization_AKA.len + auth_prefix->len + realm.len + av->authenticate.len
                    + algorithm_types[av->type].len + ck_len + ik_len
                    + registration_qop_str.len;
            x.s = pkg_malloc(x.len);
            if (!x.s) {
                LM_ERR("Error allocating %d bytes\n",
                        x.len);
                goto error;
            }
            sprintf(x.s, S_Authorization_AKA.s, auth_prefix->len, auth_prefix->s, realm.len, realm.s,
                    av->authenticate.len, av->authenticate.s,
                    algorithm_types[av->type].len, algorithm_types[av->type].s,
                    ck_len, ck, ik_len, ik, registration_qop_str.len,
                    registration_qop_str.s);
            x.len = strlen(x.s);
            break;
        case AUTH_HTTP_DIGEST_MD5:
            /* ETSI HTTP_DIGEST MD5 */
            /* this one continues into the next one */
        case AUTH_DIGEST:
            /* Cable-Labs MD5 */
            /* this one continues into the next one */
        case AUTH_SIP_DIGEST:
            /* 3GPP MD5 */
            /* this one continues into the next one */
        case AUTH_MD5:
            /* FOKUS MD5 */
            x.len = S_Authorization_MD5.len + auth_prefix->len + realm.len + av->authenticate.len
                    + algorithm_types[av->type].len + registration_qop_str.len;
            x.s = pkg_malloc(x.len);
            if (!x.s) {
                LM_ERR("pack_challenge: Error allocating %d bytes\n", x.len);
                goto error;
            }
            sprintf(x.s, S_Authorization_MD5.s, auth_prefix->len, auth_prefix->s, realm.len, realm.s,
                    av->authenticate.len, av->authenticate.s,
                    algorithm_types[AUTH_MD5].len, algorithm_types[AUTH_MD5].s,
                    registration_qop_str.len, registration_qop_str.s);
            x.len = strlen(x.s);
            break;

        default:
            LM_CRIT("not implemented for algorithm %.*s\n",
                    algorithm_types[av->type].len, algorithm_types[av->type].s);
            goto error;
    }

    if (ims_add_header_rpl(msg, &x)) {
        pkg_free(x.s);
        return 1;
    }

error:
    if (x.s)
        pkg_free(x.s);

    return 0;
}

/**
 * Adds the Authentication-Info header for, based on the credentials sent by a successful REGISTER.
 * @param msg - SIP message to add the header to
 * @returns 1 on success, 0 on error
 */
int add_authinfo_resp_hdr(struct sip_msg *msg, str nextnonce, str qop, HASHHEX rspauth, str cnonce, str nc) {

    str authinfo_hdr;
    static const char authinfo_fmt[] = "Authentication-Info: "
            "nextnonce=\"%.*s\","
            "qop=%.*s,"
            "rspauth=\"%.*s\","
            "cnonce=\"%.*s\","
            "nc=%.*s\r\n";

    authinfo_hdr.len = sizeof (authinfo_fmt) + nextnonce.len + qop.len + HASHHEXLEN + cnonce.len + nc.len - 20 /* format string parameters */ - 1 /* trailing \0 */;
    authinfo_hdr.s = pkg_malloc(authinfo_hdr.len + 1);

    if (!authinfo_hdr.s) {
        LM_ERR("add_authinfo_resp_hdr: Error allocating %d bytes\n", authinfo_hdr.len);
        goto error;
    }
    snprintf(authinfo_hdr.s, authinfo_hdr.len + 1, authinfo_fmt,
            nextnonce.len, nextnonce.s,
            qop.len, qop.s,
            HASHHEXLEN, rspauth,
            cnonce.len, cnonce.s,
            nc.len, nc.s);
    LM_DBG("authinfo hdr built: %.*s", authinfo_hdr.len, authinfo_hdr.s);
    if (ims_add_header_rpl(msg, &authinfo_hdr)) {
        LM_DBG("authinfo hdr added");
        pkg_free(authinfo_hdr.s);
        return 1;
    }
error:
    if (authinfo_hdr.s) pkg_free(authinfo_hdr.s);

    return 0;
}

/**
 * Add an authentication vector to the authentication userdata storage.
 * @param private_identity - the private identity
 * @param public_identity - the public identity
 * @param av - the authentication vector
 * @returns 1 on success or 0 on error
 */
int add_auth_vector(str private_identity, str public_identity, auth_vector * av) {
    auth_userdata *aud;
    aud = get_auth_userdata(private_identity, public_identity);
    if (!aud) goto error;

     LM_DBG("Adding auth_vector (status %d) for IMPU %.*s / IMPI %.*s (Hash %d)\n", av->status,
	public_identity.len, public_identity.s,
        private_identity.len, private_identity.s, aud->hash);


    av->prev = aud->tail;
    av->next = 0;

    if (!aud->head) aud->head = av;
    if (aud->tail) aud->tail->next = av;
    aud->tail = av;

    auth_data_unlock(aud->hash);
    return 1;
error:

    return 0;
}

/**
 * Declares all auth vectors as useless when we do a synchronization
 * @param private_identity - the private identity
 * @param public_identity - the public identity
 * @returns 1 on sucess, 0 on error
 */
int drop_auth_userdata(str private_identity, str public_identity) {
    auth_userdata *aud;
    auth_vector *av;
    aud = get_auth_userdata(private_identity, public_identity);
    if (!aud) goto error;

    av = aud->head;
    while (av) {
    	LM_DBG("dropping auth vector that was in status %d\n", av->status);
        av->status = AUTH_VECTOR_USELESS;
        av = av->next;
    }
    auth_data_unlock(aud->hash);
    return 1;
error:
	LM_DBG("no authdata to drop any auth vectors\n");
    if (aud) auth_data_unlock(aud->hash);
    return 0;
}