authorize.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2012 Smile Communications, [email protected]
  5. * Copyright (C) 2012 Smile Communications, [email protected]
  6. *
  7. * The initial version of this code was written by Dragos Vingarzan
  8. * (dragos(dot)vingarzan(at)fokus(dot)fraunhofer(dot)de and the
  9. * Fruanhofer Institute. It was and still is maintained in a separate
  10. * branch of the original SER. We are therefore migrating it to
  11. * Kamailio/SR and look forward to maintaining it from here on out.
  12. * 2011/2012 Smile Communications, Pty. Ltd.
  13. * ported/maintained/improved by
  14. * Jason Penton (jason(dot)penton(at)smilecoms.com and
  15. * Richard Good (richard(dot)good(at)smilecoms.com) as part of an
  16. * effort to add full IMS support to Kamailio/SR using a new and
  17. * improved architecture
  18. *
  19. * NB: Alot of this code was originally part of OpenIMSCore,
  20. * FhG Fokus.
  21. * Copyright (C) 2004-2006 FhG Fokus
  22. * Thanks for great work! This is an effort to
  23. * break apart the various CSCF functions into logically separate
  24. * components. We hope this will drive wider use. We also feel
  25. * that in this way the architecture is more complete and thereby easier
  26. * to manage in the Kamailio/SR environment
  27. *
  28. * This file is part of Kamailio, a free SIP server.
  29. *
  30. * Kamailio is free software; you can redistribute it and/or modify
  31. * it under the terms of the GNU General Public License as published by
  32. * the Free Software Foundation; either version 2 of the License, or
  33. * (at your option) any later version
  34. *
  35. * Kamailio is distributed in the hope that it will be useful,
  36. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. * GNU General Public License for more details.
  39. *
  40. * You should have received a copy of the GNU General Public License
  41. * along with this program; if not, write to the Free Software
  42. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  43. *
  44. */
  45. #include <string.h>
  46. #include "../../ut.h"
  47. #include "../../str.h"
  48. #include "../../basex.h"
  49. #include "../../hashes.h"
  50. #include "../../lib/srdb1/db.h"
  51. #include "../../lib/srdb1/db_ut.h"
  52. #include "../../dprint.h"
  53. #include "../../parser/digest/digest.h"
  54. #include "../../parser/hf.h"
  55. #include "../../parser/parser_f.h"
  56. #include "../../usr_avp.h"
  57. #include "../../mod_fix.h"
  58. #include "../../mem/mem.h"
  59. #include "../cdp/diameter.h"
  60. #include "../cdp/diameter_ims_code_result.h"
  61. #include "cxdx_mar.h"
  62. #include "cxdx_avp.h"
  63. #include "../../lib/ims/ims_getters.h"
  64. #include "../tm/tm_load.h"
  65. #include "api.h"
  66. #include "authims_mod.h"
  67. #include "authorize.h"
  68. #include "utils.h"
  69. #include "../../action.h" /* run_actions */
  70. extern unsigned char registration_default_algorithm_type; /**< fixed default algorithm for registration (if none present) */
  71. extern struct tm_binds tmb;
  72. extern struct cdp_binds cdpb;
  73. extern str registration_qop_str; /**< the qop options to put in the authorization challenges */
  74. extern int av_request_at_sync; /**< how many auth vectors to request in a sync MAR */
  75. extern int av_request_at_once; /**< how many auth vectors to request in a MAR */
  76. extern int auth_vector_timeout;
  77. extern int auth_data_timeout; /**< timeout for a hash entry to expire when empty in sec */
  78. extern int auth_used_vector_timeout;
  79. extern int add_authinfo_hdr;
  80. extern int max_nonce_reuse;
  81. extern str scscf_name_str;
  82. extern int ignore_failed_auth;
  83. extern int av_check_only_impu;
  84. auth_hash_slot_t *auth_data; /**< Authentication vector hash table */
  85. static int act_auth_data_hash_size = 0; /**< authentication vector hash table size */
  86. static str empty_s = {0, 0};
  87. str S_WWW = {"WWW", 3};
  88. str S_Proxy = {"Proxy", 5};
  89. str S_Authorization_AKA = {"%.*s-Authenticate: Digest realm=\"%.*s\","
  90. " nonce=\"%.*s\", algorithm=%.*s, ck=\"%.*s\", ik=\"%.*s\"%.*s\r\n", 107};
  91. str S_Authorization_MD5 = {"%.*s-Authenticate: Digest realm=\"%.*s\","
  92. " nonce=\"%.*s\", algorithm=%.*s%.*s\r\n", 102};
  93. str algorithm_types[] = {
  94. {"unknown", 7},
  95. {"AKAv1-MD5", 9},
  96. {"AKAv2-MD5", 9},
  97. {"Early-IMS", 9},
  98. {"MD5", 3},
  99. {"CableLabs-Digest", 16},
  100. {"3GPP-Digest", 11},
  101. {"TISPAN-HTTP_DIGEST_MD5", 22},
  102. {"NASS-Bundled", 12},
  103. {0, 0}
  104. };
  105. str auth_scheme_types[] = {
  106. {"unknown", 7},
  107. {"Digest-AKAv1-MD5", 16},
  108. {"Digest-AKAv2-MD5", 16},
  109. {"Early-IMS-Security", 18},
  110. {"Digest-MD5", 10},
  111. {"Digest", 6},
  112. {"SIP Digest", 10},
  113. {"HTTP_DIGEST_MD5", 15},
  114. {"NASS-Bundled", 12},
  115. {0, 0}
  116. };
  117. /**
  118. * Convert the SIP Algorithm to its type
  119. * @param algorithm - the SIP Algorithm
  120. * @returns the algorithm type
  121. */
  122. unsigned char get_algorithm_type(str algorithm) {
  123. int i;
  124. for (i = 0; algorithm_types[i].len > 0; i++)
  125. if (algorithm_types[i].len == algorithm.len
  126. && strncasecmp(algorithm_types[i].s, algorithm.s, algorithm.len)
  127. == 0)
  128. return i;
  129. return AUTH_UNKNOWN;
  130. }
  131. /**
  132. * Convert the Diameter Authorization Scheme to its type
  133. * @param scheme - the Diameter Authorization Scheme
  134. * @returns the SIP Algorithm
  135. */
  136. unsigned char get_auth_scheme_type(str scheme) {
  137. int i;
  138. for (i = 0; auth_scheme_types[i].len > 0; i++)
  139. if (auth_scheme_types[i].len == scheme.len &&
  140. strncasecmp(auth_scheme_types[i].s, scheme.s, scheme.len) == 0)
  141. return i;
  142. return AUTH_UNKNOWN;
  143. }
  144. static inline int get_ha1(struct username* _username, str* _domain,
  145. const str* _table, char* _ha1, db1_res_t** res) {
  146. return 0;
  147. }
  148. /*
  149. * Authorize digest credentials
  150. */
  151. static int digest_authenticate(struct sip_msg* msg, str *realm,
  152. str *table, hdr_types_t hftype) {
  153. return 0;
  154. }
  155. /**
  156. * Starts the reg_await_timer for an authentication vector.
  157. * @param av - the authentication vector
  158. */
  159. inline void start_reg_await_timer(auth_vector *av) {
  160. av->expires = get_ticks() + auth_vector_timeout;
  161. av->status = AUTH_VECTOR_SENT;
  162. }
  163. /**
  164. * Timer callback for reg await timers.
  165. * Drops the auth vectors that have been sent and are expired
  166. * Also drops the useless auth vectors - used and no longer needed
  167. * @param ticks - what's the time
  168. * @param param - a given parameter to be called with
  169. */
  170. void reg_await_timer(unsigned int ticks, void* param) {
  171. auth_userdata *aud, *aud_next;
  172. auth_vector *av, *av_next;
  173. int i;
  174. LM_DBG("Looking for expired/useless at %d\n", ticks);
  175. for (i = 0; i < act_auth_data_hash_size; i++) {
  176. auth_data_lock(i);
  177. aud = auth_data[i].head;
  178. while (aud) {
  179. LM_DBG("Slot %4d <%.*s>\n",
  180. aud->hash, aud->private_identity.len, aud->private_identity.s);
  181. aud_next = aud->next;
  182. av = aud->head;
  183. while (av) {
  184. LM_DBG(".. AV %4d - %d Exp %3d %p\n",
  185. av->item_number, av->status, (int) av->expires, av);
  186. av_next = av->next;
  187. if (av->status == AUTH_VECTOR_USELESS ||
  188. ((av->status == AUTH_VECTOR_USED || av->status == AUTH_VECTOR_SENT) && av->expires < ticks)
  189. ) {
  190. LM_DBG("... dropping av %d - %d\n",
  191. av->item_number, av->status);
  192. if (av->prev) av->prev->next = av->next;
  193. else aud->head = av->next;
  194. if (av->next) av->next->prev = av->prev;
  195. else aud->tail = av->prev;
  196. free_auth_vector(av);
  197. }
  198. av = av_next;
  199. }
  200. if (!aud->head) {
  201. if (aud->expires == 0) {
  202. LM_DBG("... started empty aud drop timer\n");
  203. aud->expires = ticks + auth_data_timeout;
  204. } else
  205. if (aud->expires < ticks) {
  206. LM_DBG("... dropping aud \n");
  207. if (aud->prev) aud->prev->next = aud->next;
  208. else auth_data[i].head = aud->next;
  209. if (aud->next) aud->next->prev = aud->prev;
  210. else auth_data[i].tail = aud->prev;
  211. free_auth_userdata(aud);
  212. }
  213. } else aud->expires = 0;
  214. aud = aud_next;
  215. }
  216. auth_data_unlock(i);
  217. }
  218. LM_DBG("[DONE] Looking for expired/useless at %d\n", ticks);
  219. }
  220. /*
  221. * Authenticate using Proxy-Authorize header field
  222. */
  223. /*
  224. int proxy_authenticate(struct sip_msg* _m, char* _realm, char* _table) {
  225. str srealm;
  226. str stable;
  227. if (_table == NULL) {
  228. LM_ERR("invalid table parameter\n");
  229. return -1;
  230. }
  231. stable.s = _table;
  232. stable.len = strlen(stable.s);
  233. if (get_str_fparam(&srealm, _m, (fparam_t*) _realm) < 0) {
  234. LM_ERR("failed to get realm value\n");
  235. return -1; //AUTH_ERROR;
  236. }
  237. if (srealm.len == 0) {
  238. LM_ERR("invalid realm parameter - empty value\n");
  239. return -1; //AUTH_ERROR;
  240. }
  241. LM_DBG("realm value [%.*s]\n", srealm.len, srealm.s);
  242. return digest_authenticate(_m, &srealm, &stable, HDR_PROXYAUTH_T);
  243. }
  244. */
  245. int challenge(struct sip_msg* msg, char* str1, char* alg, int is_proxy_auth, char *route) {
  246. str realm = {0, 0}, algo = {0,0};
  247. unsigned int aud_hash;
  248. str private_identity, public_identity, auts = {0, 0}, nonce = {0, 0};
  249. auth_vector *av = 0;
  250. int algo_type = 0;
  251. str route_name;
  252. saved_transaction_t* saved_t;
  253. tm_cell_t *t = 0;
  254. cfg_action_t* cfg_action;
  255. if (fixup_get_svalue(msg, (gparam_t*) route, &route_name) != 0) {
  256. LM_ERR("no async route block for assign_server_unreg\n");
  257. return -1;
  258. }
  259. if (!alg) {
  260. LM_DBG("no algorithm specified in cfg... using default\n");
  261. } else {
  262. if (get_str_fparam(&algo, msg, (fparam_t*) alg) < 0) {
  263. LM_ERR("failed to get auth algorithm\n");
  264. return -1;
  265. }
  266. }
  267. LM_DBG("Looking for route block [%.*s]\n", route_name.len, route_name.s);
  268. int ri = route_get(&main_rt, route_name.s);
  269. if (ri < 0) {
  270. LM_ERR("unable to find route block [%.*s]\n", route_name.len, route_name.s);
  271. return -1;
  272. }
  273. cfg_action = main_rt.rlist[ri];
  274. if (cfg_action == NULL) {
  275. LM_ERR("empty action lists in route block [%.*s]\n", route_name.len, route_name.s);
  276. return -1;
  277. }
  278. if (get_str_fparam(&realm, msg, (fparam_t*) str1) < 0) {
  279. LM_ERR("failed to get realm value\n");
  280. return CSCF_RETURN_ERROR;
  281. }
  282. if (realm.len == 0) {
  283. LM_ERR("invalid realm value - empty content\n");
  284. return CSCF_RETURN_ERROR;
  285. }
  286. create_return_code(CSCF_RETURN_ERROR);
  287. LM_DBG("Need to challenge for realm [%.*s]\n", realm.len, realm.s);
  288. if (msg->first_line.type != SIP_REQUEST) {
  289. LM_ERR("This message is not a request\n");
  290. return CSCF_RETURN_ERROR;
  291. }
  292. if (!is_proxy_auth) {
  293. LM_DBG("Checking if REGISTER is authorized for realm [%.*s]...\n", realm.len, realm.s);
  294. /* First check the parameters */
  295. if (msg->first_line.u.request.method.len != 8 ||
  296. memcmp(msg->first_line.u.request.method.s, "REGISTER", 8) != 0) {
  297. LM_ERR("This message is not a REGISTER request\n");
  298. return CSCF_RETURN_ERROR;
  299. }
  300. }
  301. /* get the private_identity */
  302. private_identity = cscf_get_private_identity(msg, realm);
  303. if (!private_identity.len) {
  304. LM_ERR("No private identity specified (Authorization: username)\n");
  305. stateful_request_reply(msg, 403, MSG_403_NO_PRIVATE);
  306. return CSCF_RETURN_BREAK;
  307. }
  308. /* get the public_identity */
  309. public_identity = cscf_get_public_identity(msg);
  310. if (!public_identity.len) {
  311. LM_ERR("No public identity specified (To:)\n");
  312. stateful_request_reply(msg, 403, MSG_403_NO_PUBLIC);
  313. return CSCF_RETURN_BREAK;
  314. }
  315. if (algo.len > 0) {
  316. algo_type = get_algorithm_type(algo);
  317. } else {
  318. algo_type = registration_default_algorithm_type;
  319. }
  320. // /* check if it is a synchronization request */
  321. // //TODO this is MAR syncing - have removed it currently - TOD maybe put back in
  322. // auts = ims_get_auts(msg, realm, is_proxy_auth);
  323. // if (auts.len) {
  324. // LM_DBG("IMS Auth Synchronization requested <%.*s>\n", auts.len, auts.s);
  325. //
  326. // nonce = ims_get_nonce(msg, realm);
  327. // if (nonce.len == 0) {
  328. // LM_DBG("Nonce not found (Authorization: nonce)\n");
  329. // stateful_request_reply(msg, 403, MSG_403_NO_NONCE);
  330. // return CSCF_RETURN_BREAK;
  331. // }
  332. // av = get_auth_vector(private_identity, public_identity, AUTH_VECTOR_USED, &nonce, &aud_hash);
  333. // if (!av)
  334. // av = get_auth_vector(private_identity, public_identity, AUTH_VECTOR_SENT, &nonce, &aud_hash);
  335. //
  336. // if (!av) {
  337. // LM_ERR("nonce not recognized as sent, no sync!\n");
  338. // auts.len = 0;
  339. // auts.s = 0;
  340. // } else {
  341. // av->status = AUTH_VECTOR_USELESS;
  342. // auth_data_unlock(aud_hash);
  343. // av = 0;
  344. // resync = 1;
  345. // }
  346. // }
  347. //RICHARD changed this
  348. //Previous approach sent MAR, got MAA then put auth vectors into queue
  349. //Then we try and get that auth vector out the queue (it might be used by someone else so we loop)
  350. //new approach
  351. //we do MAR get MAA (asynchronously) get auth vector use it to pack the vector etc.
  352. //set it to sent and set an expires on it
  353. //then add it to the queue!
  354. /* loop because some other process might steal the auth_vector that we just retrieved */
  355. //while (!(av = get_auth_vector(private_identity, public_identity, AUTH_VECTOR_UNUSED, 0, &aud_hash))) {
  356. if ((av = get_auth_vector(private_identity, public_identity, AUTH_VECTOR_UNUSED, 0, &aud_hash))) {
  357. if (!av) {
  358. LM_ERR("Error retrieving an auth vector\n");
  359. return CSCF_RETURN_ERROR;
  360. }
  361. if (!pack_challenge(msg, realm, av, is_proxy_auth)) {
  362. stateful_request_reply(msg, 500, MSG_500_PACK_AV);
  363. auth_data_unlock(aud_hash);
  364. return CSCF_RETURN_ERROR;
  365. }
  366. start_reg_await_timer(av); //start the timer to remove stale or unused Auth Vectors
  367. if (is_proxy_auth) {
  368. stateful_request_reply(msg, 407, MSG_407_CHALLENGE);
  369. } else {
  370. stateful_request_reply(msg, 401, MSG_401_CHALLENGE);
  371. }
  372. auth_data_unlock(aud_hash);
  373. } else {
  374. //before we send lets suspend the transaction
  375. t = tmb.t_gett();
  376. if (t == NULL || t == T_UNDEFINED) {
  377. if (tmb.t_newtran(msg) < 0) {
  378. LM_ERR("cannot create the transaction for MAR async\n");
  379. stateful_request_reply(msg, 480, MSG_480_DIAMETER_ERROR);
  380. return CSCF_RETURN_BREAK;
  381. }
  382. t = tmb.t_gett();
  383. if (t == NULL || t == T_UNDEFINED) {
  384. LM_ERR("cannot lookup the transaction\n");
  385. stateful_request_reply(msg, 480, MSG_480_DIAMETER_ERROR);
  386. return CSCF_RETURN_BREAK;
  387. }
  388. }
  389. saved_t = shm_malloc(sizeof (saved_transaction_t));
  390. if (!saved_t) {
  391. LM_ERR("no more memory trying to save transaction state\n");
  392. return CSCF_RETURN_ERROR;
  393. }
  394. memset(saved_t, 0, sizeof (saved_transaction_t));
  395. saved_t->act = cfg_action;
  396. saved_t->realm.s = (char*) shm_malloc(realm.len + 1);
  397. if (!saved_t->realm.s) {
  398. LM_ERR("no more memory trying to save transaction state : callid\n");
  399. shm_free(saved_t);
  400. return CSCF_RETURN_ERROR;
  401. }
  402. memset(saved_t->realm.s, 0, realm.len + 1);
  403. memcpy(saved_t->realm.s, realm.s, realm.len);
  404. saved_t->realm.len = realm.len;
  405. saved_t->is_proxy_auth = is_proxy_auth;
  406. LM_DBG("Suspending SIP TM transaction\n");
  407. if (tmb.t_suspend(msg, &saved_t->tindex, &saved_t->tlabel) < 0) {
  408. LM_ERR("failed to suspend the TM processing\n");
  409. free_saved_transaction_data(saved_t);
  410. stateful_request_reply(msg, 480, MSG_480_DIAMETER_ERROR);
  411. return CSCF_RETURN_BREAK;
  412. }
  413. if (multimedia_auth_request(msg, public_identity, private_identity, av_request_at_once,
  414. auth_scheme_types[algo_type], nonce, auts, scscf_name_str, saved_t)!=0) {
  415. LM_ERR("ERR:I_MAR: Error sending MAR or MAR time-out\n");
  416. tmb.t_cancel_suspend(saved_t->tindex, saved_t->tlabel);
  417. free_saved_transaction_data(saved_t);
  418. stateful_request_reply(msg, 480, MSG_480_DIAMETER_ERROR);
  419. return CSCF_RETURN_BREAK;
  420. }
  421. }
  422. return CSCF_RETURN_BREAK;
  423. }
  424. int www_challenge2(struct sip_msg* msg, char* _route, char* str1, char* str2) {
  425. return challenge(msg, str1, 0, 0, _route);
  426. }
  427. int www_challenge3(struct sip_msg* msg, char* _route, char* str1, char* str2) {
  428. return challenge(msg, str1, str2, 0, _route);
  429. }
  430. int www_resync_auth(struct sip_msg* msg, char* _route, char* str1, char* str2) {
  431. str realm = {0, 0};
  432. unsigned int aud_hash;
  433. str private_identity, public_identity, auts = {0, 0}, nonce = {0, 0};
  434. auth_vector *av = 0;
  435. int algo_type;
  436. int is_proxy_auth=0;
  437. str route_name;
  438. saved_transaction_t* saved_t;
  439. tm_cell_t *t = 0;
  440. cfg_action_t* cfg_action;
  441. if (fixup_get_svalue(msg, (gparam_t*) _route, &route_name) != 0) {
  442. LM_ERR("no async route block for assign_server_unreg\n");
  443. return -1;
  444. }
  445. LM_DBG("Looking for route block [%.*s]\n", route_name.len, route_name.s);
  446. int ri = route_get(&main_rt, route_name.s);
  447. if (ri < 0) {
  448. LM_ERR("unable to find route block [%.*s]\n", route_name.len, route_name.s);
  449. return -1;
  450. }
  451. cfg_action = main_rt.rlist[ri];
  452. if (cfg_action == NULL) {
  453. LM_ERR("empty action lists in route block [%.*s]\n", route_name.len, route_name.s);
  454. return -1;
  455. }
  456. if (get_str_fparam(&realm, msg, (fparam_t*) str1) < 0) {
  457. LM_ERR("failed to get realm value\n");
  458. return CSCF_RETURN_ERROR;
  459. }
  460. if (realm.len == 0) {
  461. LM_ERR("invalid realm value - empty content\n");
  462. return CSCF_RETURN_ERROR;
  463. }
  464. create_return_code(CSCF_RETURN_ERROR);
  465. if (msg->first_line.type != SIP_REQUEST) {
  466. LM_ERR("This message is not a request\n");
  467. return CSCF_RETURN_ERROR;
  468. }
  469. /* get the private_identity */
  470. private_identity = cscf_get_private_identity(msg, realm);
  471. if (!private_identity.len) {
  472. LM_ERR("No private identity specified (Authorization: username)\n");
  473. stateful_request_reply(msg, 403, MSG_403_NO_PRIVATE);
  474. return CSCF_RETURN_BREAK;
  475. }
  476. /* get the public_identity */
  477. public_identity = cscf_get_public_identity(msg);
  478. if (!public_identity.len) {
  479. LM_ERR("No public identity specified (To:)\n");
  480. stateful_request_reply(msg, 403, MSG_403_NO_PUBLIC);
  481. return CSCF_RETURN_BREAK;
  482. }
  483. algo_type = registration_default_algorithm_type;
  484. /* check if it is a synchronization request */
  485. //TODO this is MAR syncing - have removed it currently - TOD maybe put back in
  486. auts = ims_get_auts(msg, realm, is_proxy_auth);
  487. if (auts.len) {
  488. LM_DBG("IMS Auth Synchronization requested <%.*s>\n", auts.len, auts.s);
  489. nonce = ims_get_nonce(msg, realm);
  490. if (nonce.len == 0) {
  491. LM_DBG("Nonce not found (Authorization: nonce)\n");
  492. stateful_request_reply(msg, 403, MSG_403_NO_NONCE);
  493. return CSCF_RETURN_BREAK;
  494. }
  495. av = get_auth_vector(private_identity, public_identity, AUTH_VECTOR_USED, &nonce, &aud_hash);
  496. if (!av)
  497. av = get_auth_vector(private_identity, public_identity, AUTH_VECTOR_SENT, &nonce, &aud_hash);
  498. if (!av) {
  499. LM_ERR("nonce not recognized as sent, no sync!\n");
  500. auts.len = 0;
  501. auts.s = 0;
  502. } else {
  503. av->status = AUTH_VECTOR_USELESS;
  504. auth_data_unlock(aud_hash);
  505. av = 0;
  506. }
  507. }
  508. //before we send lets suspend the transaction
  509. t = tmb.t_gett();
  510. if (t == NULL || t == T_UNDEFINED) {
  511. if (tmb.t_newtran(msg) < 0) {
  512. LM_ERR("cannot create the transaction for MAR async\n");
  513. stateful_request_reply(msg, 480, MSG_480_DIAMETER_ERROR);
  514. return CSCF_RETURN_BREAK;
  515. }
  516. t = tmb.t_gett();
  517. if (t == NULL || t == T_UNDEFINED) {
  518. LM_ERR("cannot lookup the transaction\n");
  519. stateful_request_reply(msg, 480, MSG_480_DIAMETER_ERROR);
  520. return CSCF_RETURN_BREAK;
  521. }
  522. }
  523. saved_t = shm_malloc(sizeof(saved_transaction_t));
  524. if (!saved_t) {
  525. LM_ERR("no more memory trying to save transaction state\n");
  526. return CSCF_RETURN_ERROR;
  527. }
  528. memset(saved_t, 0, sizeof(saved_transaction_t));
  529. saved_t->act = cfg_action;
  530. saved_t->realm.s = (char*) shm_malloc(realm.len + 1);
  531. if (!saved_t->realm.s) {
  532. LM_ERR("no more memory trying to save transaction state : callid\n");
  533. shm_free(saved_t);
  534. return CSCF_RETURN_ERROR;
  535. }
  536. memset(saved_t->realm.s, 0, realm.len + 1);
  537. memcpy(saved_t->realm.s, realm.s, realm.len);
  538. saved_t->realm.len = realm.len;
  539. saved_t->is_proxy_auth = is_proxy_auth;
  540. saved_t->is_resync = 1;
  541. LM_DBG("Suspending SIP TM transaction\n");
  542. if (tmb.t_suspend(msg, &saved_t->tindex, &saved_t->tlabel) < 0) {
  543. LM_ERR("failed to suspend the TM processing\n");
  544. free_saved_transaction_data(saved_t);
  545. stateful_request_reply(msg, 480, MSG_480_DIAMETER_ERROR);
  546. return CSCF_RETURN_BREAK;
  547. }
  548. if (multimedia_auth_request(msg, public_identity, private_identity,
  549. av_request_at_sync, auth_scheme_types[algo_type], nonce, auts,
  550. scscf_name_str, saved_t) != 0) {
  551. LM_ERR("ERR:I_MAR: Error sending MAR or MAR time-out\n");
  552. tmb.t_cancel_suspend(saved_t->tindex, saved_t->tlabel);
  553. free_saved_transaction_data(saved_t);
  554. stateful_request_reply(msg, 480, MSG_480_DIAMETER_ERROR);
  555. return CSCF_RETURN_BREAK;
  556. }
  557. return CSCF_RETURN_BREAK;
  558. }
  559. int proxy_challenge(struct sip_msg* msg, char* _route, char* str1, char* str2) {
  560. return challenge(msg, str1, str2, 1, _route);
  561. }
  562. /**
  563. * Replies to a REGISTER and also adds the need headers
  564. * Path and Service-Route are added.
  565. * @param msg - the SIP message to operator on
  566. * @param code - Reason Code for the response
  567. * @param text - Reason Phrase for the response
  568. * @returns #CSCF_RETURN_TRUE on success or #CSCF_RETURN_FALSE if not added
  569. */
  570. int stateful_request_reply(struct sip_msg *msg, int code, char *text) {
  571. unsigned int hash, label;
  572. struct hdr_field *h;
  573. str t = {0, 0};
  574. if (parse_headers(msg, HDR_EOH_F, 0) < 0) {
  575. LM_ERR("Error parsing headers\n");
  576. return -1;
  577. }
  578. h = msg->headers;
  579. while (h) {
  580. if (h->name.len == 4 &&
  581. strncasecmp(h->name.s, "Path", 4) == 0) {
  582. t.s = h->name.s;
  583. t.len = h->len;
  584. ims_add_header_rpl(msg, &(t));
  585. }
  586. h = h->next;
  587. }
  588. /*if (code==200){
  589. ims_add_header_rpl(msg,&scscf_service_route);
  590. }*/ //TODO: need to get the service route from somewhere - registrar?
  591. if (tmb.t_get_trans_ident(msg, &hash, &label) < 0) {
  592. if (tmb.t_newtran(msg) < 0)
  593. LM_INFO("Failed creating SIP transaction\n");
  594. }
  595. return tmb.t_reply(msg, code, text);
  596. }
  597. /**
  598. * Replies to a REGISTER and also adds the need headers
  599. * Path and Service-Route are added.
  600. * @param msg - the SIP message to operator on
  601. * @param code - Reason Code for the response
  602. * @param text - Reason Phrase for the response
  603. * @returns #CSCF_RETURN_TRUE on success or #CSCF_RETURN_FALSE if not added
  604. */
  605. int stateful_request_reply_async(struct cell* t_cell, struct sip_msg *msg, int code, char *text) {
  606. struct hdr_field *h;
  607. str t = {0, 0};
  608. if (parse_headers(msg, HDR_EOH_F, 0) < 0) {
  609. LM_ERR("Error parsing headers\n");
  610. return -1;
  611. }
  612. h = msg->headers;
  613. while (h) {
  614. if (h->name.len == 4 &&
  615. strncasecmp(h->name.s, "Path", 4) == 0) {
  616. t.s = h->name.s;
  617. t.len = h->len;
  618. ims_add_header_rpl(msg, &(t));
  619. }
  620. h = h->next;
  621. }
  622. return tmb.t_reply_trans(t_cell, msg, code, text);
  623. }
  624. int authenticate(struct sip_msg* msg, char* _realm, char* str2, int is_proxy_auth) {
  625. int ret = -1; //CSCF_RETURN_FALSE;
  626. unsigned int aud_hash = 0;
  627. str realm;
  628. str private_identity, public_identity;
  629. str nonce, response16, nc, cnonce, qop_str = {0, 0}, auts = {0, 0}, body, *next_nonce = &empty_s;
  630. enum qop_type qop = QOP_UNSPEC;
  631. str uri = {0, 0};
  632. HASHHEX expected, ha1, hbody, rspauth;
  633. int expected_len = 32;
  634. int expires = 0;
  635. auth_vector *av = 0;
  636. uint32_t nc_parsed = 0; /* the numerical representation of nc */
  637. ret = AUTH_ERROR;
  638. if (get_str_fparam(&realm, msg, (fparam_t*) _realm) < 0) {
  639. LM_ERR("failed to get realm value\n");
  640. return AUTH_NO_CREDENTIALS;
  641. }
  642. if (realm.len == 0) {
  643. LM_ERR("invalid realm value - empty content\n");
  644. return AUTH_NO_CREDENTIALS;
  645. }
  646. if (msg->first_line.type != SIP_REQUEST) {
  647. LM_ERR("This message is not a request\n");
  648. ret = AUTH_ERROR;
  649. goto end;
  650. }
  651. if (!is_proxy_auth) {
  652. LM_DBG("Checking if REGISTER is authorized for realm [%.*s]...\n", realm.len, realm.s);
  653. /* First check the parameters */
  654. if (msg->first_line.u.request.method.len != 8 ||
  655. memcmp(msg->first_line.u.request.method.s, "REGISTER", 8) != 0) {
  656. LM_ERR("This message is not a REGISTER request\n");
  657. ret = AUTH_ERROR;
  658. goto end;
  659. }
  660. }
  661. if (!realm.len) {
  662. LM_ERR("No realm found\n");
  663. return 0; //CSCF_RETURN_BREAK;
  664. }
  665. private_identity = cscf_get_private_identity(msg, realm);
  666. if (!private_identity.len) {
  667. LM_ERR("private identity missing\n");
  668. return AUTH_NO_CREDENTIALS;
  669. }
  670. public_identity = cscf_get_public_identity(msg);
  671. if (!public_identity.len) {
  672. LM_ERR("public identity missing\n");
  673. return AUTH_NO_CREDENTIALS;
  674. }
  675. if (!get_nonce_response(msg, realm, &nonce, &response16, &qop, &qop_str, &nc, &cnonce, &uri, is_proxy_auth) ||
  676. !nonce.len || !response16.len) {
  677. LM_DBG("Nonce or response missing: nonce len [%i], response16 len[%i]\n", nonce.len, response16.len);
  678. return AUTH_ERROR;
  679. }
  680. if (qop == QOP_AUTHINT) {
  681. body = ims_get_body(msg);
  682. calc_H(&body, hbody);
  683. }
  684. /* first, look for an already used vector (if nonce reuse is enabled) */
  685. if (max_nonce_reuse > 0) {
  686. LM_DBG("look for an already used vector for %.*s\n",
  687. private_identity.len, private_identity.s);
  688. av = get_auth_vector(private_identity, public_identity, AUTH_VECTOR_USED, &nonce, &aud_hash);
  689. }
  690. if (!av) {
  691. /* if none found, or nonce reuse is disabled, look for a fresh vector
  692. * We should also drop every other used vector at this point
  693. * (there souldn't be more than one) */
  694. 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);
  695. auth_userdata *aud;
  696. auth_vector *av_it;
  697. aud = get_auth_userdata(private_identity, public_identity);
  698. if (aud) {
  699. av_it = aud->head;
  700. while (av_it) {
  701. if (av_it->status == AUTH_VECTOR_USED) {
  702. LM_DBG("vector %p is marked for deletion\n", av_it);
  703. av_it->status = AUTH_VECTOR_USELESS;
  704. }
  705. av_it = av_it->next;
  706. }
  707. auth_data_unlock(aud->hash);
  708. }
  709. LM_DBG("look for a fresh vector for %.*s\n",
  710. private_identity.len, private_identity.s);
  711. av = get_auth_vector(private_identity, public_identity, AUTH_VECTOR_SENT, &nonce, &aud_hash);
  712. }
  713. LM_INFO("uri=%.*s nonce=%.*s response=%.*s qop=%.*s nc=%.*s cnonce=%.*s hbody=%.*s\n",
  714. uri.len, uri.s,
  715. nonce.len, nonce.s,
  716. response16.len, response16.s,
  717. qop_str.len, qop_str.s,
  718. nc.len, nc.s,
  719. cnonce.len, cnonce.s,
  720. 32, hbody);
  721. if (!av) {
  722. LM_DBG("no matching auth vector found - maybe timer expired\n");
  723. if (ignore_failed_auth) {
  724. LM_WARN("NB: Ignoring all failed auth - check your config if you don't expect this\n");
  725. ret = AUTH_OK;
  726. }
  727. goto end;
  728. }
  729. if (qop != QOP_UNSPEC) {
  730. /* if QOP is sent, nc must be specified */
  731. /* the expected nc is the last used one plus 1 */
  732. int p;
  733. for (p = 0; p < 8; ++p) { /* nc is 8LHEX (RFC 2617 §3.2.2) */
  734. nc_parsed = (nc_parsed << 4) | UNHEX((int) nc.s[p]);
  735. }
  736. LM_DBG("nc is %08x, expected: %08x\n",
  737. nc_parsed, av->use_nb + 1);
  738. if (nc_parsed <= av->use_nb) { /* nc is lower than expected */
  739. ret = AUTH_NONCE_REUSED;
  740. av->status = AUTH_VECTOR_USELESS; /* invalidate this vector if any mistake/error occurs */
  741. goto cleanup;
  742. } else if (nc_parsed > av->use_nb + 1) { /* nc is bigger than expected */
  743. ret = AUTH_ERROR;
  744. av->status = AUTH_VECTOR_USELESS;
  745. goto cleanup;
  746. }
  747. }
  748. switch (av->type) {
  749. case AUTH_AKAV1_MD5:
  750. case AUTH_AKAV2_MD5:
  751. case AUTH_MD5:
  752. calc_HA1(HA_MD5, &private_identity, &realm, &(av->authorization), &(av->authenticate), &cnonce, ha1);
  753. calc_response(ha1, &(av->authenticate),
  754. &nc,
  755. &cnonce,
  756. &qop_str,
  757. qop == QOP_AUTHINT,
  758. &msg->first_line.u.request.method, &uri, hbody, expected);
  759. LM_INFO("UE said: %.*s and we expect %.*s ha1 %.*s (%.*s)\n",
  760. response16.len, response16.s,
  761. /*av->authorization.len,av->authorization.s,*/32, expected,
  762. 32, ha1,
  763. msg->first_line.u.request.method.len, msg->first_line.u.request.method.s);
  764. break;
  765. case AUTH_SIP_DIGEST:
  766. case AUTH_DIGEST:
  767. // memcpy of received HA1
  768. memcpy(ha1, av->authorization.s, HASHHEXLEN);
  769. calc_response(ha1, &(av->authenticate),
  770. &nc,
  771. &cnonce,
  772. &qop_str,
  773. qop == QOP_AUTHINT,
  774. &msg->first_line.u.request.method, &uri, hbody, expected);
  775. LM_INFO("UE said: %.*s and we expect %.*s ha1 %.*s (%.*s)\n",
  776. response16.len, response16.s,
  777. 32,expected,
  778. 32,ha1,
  779. msg->first_line.u.request.method.len, msg->first_line.u.request.method.s);
  780. break;
  781. default:
  782. LM_ERR("algorithm %.*s is not handled.\n",
  783. algorithm_types[av->type].len, algorithm_types[av->type].s);
  784. ret = AUTH_ERROR;
  785. goto cleanup; /* release aud before returning */
  786. }
  787. expires = cscf_get_max_expires(msg, 0);
  788. if (response16.len == expected_len && strncasecmp(response16.s, expected, response16.len) == 0) {
  789. if (max_nonce_reuse > 0 && av->status == AUTH_VECTOR_SENT) {
  790. /* first use of a reusable vector */
  791. /* set the vector's new timeout */
  792. LM_DBG("vector %p now expires in %d seconds\n", av, auth_used_vector_timeout);
  793. av->expires = get_ticks() + auth_used_vector_timeout;
  794. }
  795. av->use_nb++;
  796. LM_DBG("vector %p successfully used %d time(s)\n", av, av->use_nb);
  797. if (av->use_nb == max_nonce_reuse + 1) {
  798. LM_DBG("vector %p isn't fresh anymore, recycle it with a new nonce\n", av);
  799. int i;
  800. char y[NONCE_LEN];
  801. for (i = 0; i < NONCE_LEN; i++)
  802. y[i] = (unsigned char) ((int) (256.0 * rand() / (RAND_MAX + 1.0)));
  803. if (unlikely((av->authenticate.len < 2 * NONCE_LEN))) {
  804. if (av->authenticate.s) {
  805. shm_free(av->authenticate.s);
  806. }
  807. av->authenticate.len = 2 * NONCE_LEN;
  808. av->authenticate.s = shm_malloc(av->authenticate.len);
  809. }
  810. if (!av->authenticate.s) {
  811. LM_ERR("new_auth_vector: failed allocating %d bytes!\n", av->authenticate.len);
  812. av->authenticate.len = 0;
  813. goto cleanup;
  814. }
  815. av->authenticate.len = bin_to_base16(y, NONCE_LEN, av->authenticate.s);
  816. next_nonce = &(av->authenticate);
  817. av->status = AUTH_VECTOR_USED;
  818. av->use_nb = 0;
  819. av->expires = get_ticks() + auth_used_vector_timeout; /* reset the timer */
  820. } else if (expires == 0) { /* de-registration */
  821. LM_DBG("de-registration, vector %p isn't needed anymore\n", av);
  822. av->status = AUTH_VECTOR_USELESS;
  823. } else {
  824. av->status = AUTH_VECTOR_USED;
  825. /* nextnonce is the current nonce */
  826. next_nonce = &nonce;
  827. }
  828. ret = AUTH_OK;
  829. if (add_authinfo_hdr && expires != 0 /* don't add auth. info if de-registation */) {
  830. /* calculate rspauth */
  831. calc_response(ha1, &nonce,
  832. &nc,
  833. &cnonce,
  834. &qop_str,
  835. qop == QOP_AUTHINT,
  836. 0, &uri, hbody, rspauth);
  837. add_authinfo_resp_hdr(msg, *next_nonce, qop_str, rspauth, cnonce, nc);
  838. }
  839. } else {
  840. char authorise[200];
  841. char authenticate_bin[200];
  842. char authenticate_hex[200];
  843. memset(authorise, 0, 200);
  844. memset(authenticate_bin, 0, 200);
  845. memset(authenticate_hex, 0, 200);
  846. int authorise_len = bin_to_base16(av->authorization.s, av->authorization.len, authorise);
  847. int authenticate_len = base64_to_bin(av->authenticate.s, av->authenticate.len, authenticate_bin);
  848. int authenticate_hex_len = bin_to_base16(authenticate_bin, authenticate_len, authenticate_hex);
  849. av->status = AUTH_VECTOR_USELESS; /* first mistake, you're out! (but maybe it's synchronization) */
  850. LM_DBG("UE said: %.*s, but we expect %.*s : authenticate(b64) is [%.*s], authenticate(hex) is [%.*s], authorise is [%d] [%.*s]\n",
  851. response16.len, response16.s,
  852. 32, expected,
  853. av->authenticate.len, av->authenticate.s,
  854. authenticate_hex_len,authenticate_hex,
  855. authorise_len,
  856. authorise_len, authorise);
  857. // /* check for auts in authorization header - if it is then we need to resync */
  858. auts = ims_get_auts(msg, realm, is_proxy_auth);
  859. if (auts.len) {
  860. LM_DBG("IMS Auth Synchronization requested <%.*s>\n", auts.len, auts.s);
  861. ret = AUTH_RESYNC_REQUESTED;
  862. av->status = AUTH_VECTOR_SENT;
  863. } else {
  864. ret = AUTH_INVALID_PASSWORD;
  865. }
  866. }
  867. if (ignore_failed_auth) {
  868. LM_WARN("NB: Ignoring all failed auth - check your config if you don't expect this\n");
  869. ret = AUTH_OK;
  870. }
  871. cleanup:
  872. auth_data_unlock(aud_hash);
  873. end:
  874. return ret;
  875. }
  876. /*
  877. * Authenticate using WWW-Authorize header field
  878. */
  879. int www_authenticate(struct sip_msg* msg, char* _realm, char* str2) {
  880. return authenticate(msg, _realm, str2, 0);
  881. }
  882. /*
  883. * Authenticate using WWW-Authorize header field
  884. */
  885. int proxy_authenticate(struct sip_msg* msg, char* _realm, char* str2) {
  886. return authenticate(msg, _realm, str2, 1);
  887. }
  888. /**
  889. * @brief bind functions to IMS AUTH API structure
  890. */
  891. int bind_ims_auth(ims_auth_api_t * api) {
  892. if (!api) {
  893. ERR("Invalid parameter value\n");
  894. return -1;
  895. }
  896. api->digest_authenticate = digest_authenticate;
  897. return 0;
  898. }
  899. /**
  900. * Retrieve an authentication vector.
  901. * \note returns with a lock, so unlock it when done
  902. * @param private_identity - the private identity
  903. * @param public_identity - the public identity
  904. * @param status - the status of the authentication vector
  905. * @param nonce - the nonce in the auth vector
  906. * @param hash - the hash to unlock when done
  907. * @returns the auth_vector* if found or NULL if not
  908. */
  909. auth_vector * get_auth_vector(str private_identity, str public_identity, int status, str *nonce, unsigned int *hash) {
  910. auth_userdata *aud;
  911. auth_vector *av;
  912. aud = get_auth_userdata(private_identity, public_identity);
  913. if (!aud) {
  914. LM_ERR("no auth userdata\n");
  915. goto error;
  916. }
  917. av = aud->head;
  918. while (av) {
  919. LM_DBG("looping through AV status is %d and were looking for %d\n", av->status, status);
  920. if (av->status == status && (nonce == 0 || (nonce->len == av->authenticate.len && memcmp(nonce->s, av->authenticate.s, nonce->len) == 0))) {
  921. LM_DBG("Found result\n");
  922. *hash = aud->hash;
  923. return av;
  924. }
  925. av = av->next;
  926. }
  927. error:
  928. if (aud) auth_data_unlock(aud->hash);
  929. return 0;
  930. }
  931. /**
  932. * Locks the required slot of the auth_data.
  933. * @param hash - the index of the slot
  934. */
  935. inline void auth_data_lock(unsigned int hash) {
  936. lock_get(auth_data[(hash)].lock);
  937. }
  938. /**
  939. * UnLocks the required slot of the auth_data
  940. * @param hash - the index of the slot
  941. */
  942. inline void auth_data_unlock(unsigned int hash) {
  943. lock_release(auth_data[(hash)].lock);
  944. }
  945. /**
  946. * Initializes the Authorization Data structures.
  947. * @param size - size of the hash table
  948. * @returns 1 on success or 0 on error
  949. */
  950. int auth_data_init(int size) {
  951. int i;
  952. auth_data = shm_malloc(sizeof (auth_hash_slot_t) * size);
  953. if (!auth_data) {
  954. LM_ERR("error allocating mem\n");
  955. return 0;
  956. }
  957. memset(auth_data, 0, sizeof (auth_hash_slot_t) * size);
  958. for (i = 0; i < size; i++) {
  959. auth_data[i].lock = lock_alloc();
  960. lock_init(auth_data[i].lock);
  961. }
  962. act_auth_data_hash_size = size;
  963. return 1;
  964. }
  965. /**
  966. * Destroy the Authorization Data structures */
  967. void auth_data_destroy() {
  968. int i;
  969. auth_userdata *aud, *next;
  970. for (i = 0; i < act_auth_data_hash_size; i++) {
  971. auth_data_lock(i);
  972. lock_destroy(auth_data[i].lock);
  973. lock_dealloc(auth_data[i].lock);
  974. aud = auth_data[i].head;
  975. while (aud) {
  976. next = aud->next;
  977. free_auth_userdata(aud);
  978. aud = next;
  979. }
  980. }
  981. if (auth_data) shm_free(auth_data);
  982. }
  983. /**
  984. * Create new authorization vector
  985. * @param item_number - number to index it in the vectors list
  986. * @param auth_scheme - Diameter Authorization Scheme
  987. * @param authenticate - the challenge
  988. * @param authorization - the expected response
  989. * @param ck - the cypher key
  990. * @param ik - the integrity key
  991. * @returns the new auth_vector* or NULL on error
  992. */
  993. auth_vector * new_auth_vector(int item_number, str auth_scheme, str authenticate,
  994. str authorization, str ck, str ik) {
  995. auth_vector *x = 0;
  996. x = shm_malloc(sizeof (auth_vector));
  997. if (!x) {
  998. LM_ERR("error allocating mem\n");
  999. goto done;
  1000. }
  1001. memset(x, 0, sizeof (auth_vector));
  1002. x->item_number = item_number;
  1003. x->type = get_auth_scheme_type(auth_scheme);
  1004. switch (x->type) {
  1005. case AUTH_AKAV1_MD5:
  1006. case AUTH_AKAV2_MD5:
  1007. /* AKA */
  1008. x->authenticate.len = authenticate.len * 4 / 3 + 4;
  1009. x->authenticate.s = shm_malloc(x->authenticate.len);
  1010. if (!x->authenticate.s) {
  1011. LM_ERR("error allocating mem\n");
  1012. goto done;
  1013. }
  1014. x->authenticate.len = bin_to_base64(authenticate.s, authenticate.len,
  1015. x->authenticate.s);
  1016. x->authorization.len = authorization.len;
  1017. x->authorization.s = shm_malloc(x->authorization.len);
  1018. if (!x->authorization.s) {
  1019. LM_ERR("error allocating mem\n");
  1020. goto done;
  1021. }
  1022. memcpy(x->authorization.s, authorization.s, authorization.len);
  1023. x->ck.len = ck.len;
  1024. x->ck.s = shm_malloc(ck.len);
  1025. if (!x->ck.s) {
  1026. LM_ERR("error allocating mem\n");
  1027. goto done;
  1028. }
  1029. memcpy(x->ck.s, ck.s, ck.len);
  1030. x->ik.len = ik.len;
  1031. x->ik.s = shm_malloc(ik.len);
  1032. if (!x->ik.s) {
  1033. LM_ERR("error allocating mem\n");
  1034. goto done;
  1035. }
  1036. memcpy(x->ik.s, ik.s, ik.len);
  1037. break;
  1038. case AUTH_MD5:
  1039. /* MD5 */
  1040. x->authenticate.len = authenticate.len * 2;
  1041. x->authenticate.s = shm_malloc(x->authenticate.len);
  1042. if (!x->authenticate.s) {
  1043. LM_ERR("new_auth_vector: error allocating mem\n");
  1044. goto done;
  1045. }
  1046. x->authenticate.len = bin_to_base16(authenticate.s, authenticate.len,
  1047. x->authenticate.s);
  1048. x->authorization.len = authorization.len;
  1049. x->authorization.s = shm_malloc(x->authorization.len);
  1050. if (!x->authorization.s) {
  1051. LM_ERR("new_auth_vector: error allocating mem\n");
  1052. goto done;
  1053. }
  1054. memcpy(x->authorization.s, authorization.s, authorization.len);
  1055. x->authorization.len = authorization.len;
  1056. break;
  1057. case AUTH_DIGEST:
  1058. case AUTH_SIP_DIGEST:
  1059. {
  1060. int i;
  1061. char y[NONCE_LEN];
  1062. for (i = 0; i < NONCE_LEN; i++)
  1063. y[i] = (unsigned char) ((int) (256.0 * rand() / (RAND_MAX + 1.0)));
  1064. x->authenticate.len = 2 * NONCE_LEN;
  1065. x->authenticate.s = shm_malloc(x->authenticate.len);
  1066. if (!x->authenticate.s) {
  1067. LM_ERR("new_auth_vector: failed allocating %d bytes!\n", x->authenticate.len);
  1068. x->authenticate.len = 0;
  1069. goto done;
  1070. }
  1071. x->authenticate.len = bin_to_base16(y, NONCE_LEN, x->authenticate.s);
  1072. }
  1073. x->authorization.len = authorization.len;
  1074. x->authorization.s = shm_malloc(x->authorization.len);
  1075. if (!x->authorization.s) {
  1076. LM_ERR("new_auth_vector: error allocating mem\n");
  1077. x->authorization.len = 0;
  1078. goto done;
  1079. }
  1080. memcpy(x->authorization.s, authorization.s, authorization.len);
  1081. x->authorization.len = authorization.len;
  1082. break;
  1083. case AUTH_HTTP_DIGEST_MD5:
  1084. x->authenticate.len = authenticate.len;
  1085. x->authenticate.s = shm_malloc(x->authenticate.len);
  1086. if (!x->authenticate.s) {
  1087. LM_ERR("new_auth_vector: error allocating mem\n");
  1088. x->authenticate.len = 0;
  1089. goto done;
  1090. }
  1091. memcpy(x->authenticate.s, authenticate.s, authenticate.len);
  1092. x->authorization.len = authorization.len;
  1093. x->authorization.s = shm_malloc(x->authorization.len);
  1094. if (!x->authorization.s) {
  1095. LM_ERR("new_auth_vector: error allocating mem\n");
  1096. x->authorization.len = 0;
  1097. goto done;
  1098. }
  1099. memcpy(x->authorization.s, authorization.s, authorization.len);
  1100. break;
  1101. case AUTH_EARLY_IMS:
  1102. /* early IMS */
  1103. x->authenticate.len = 0;
  1104. x->authenticate.s = 0;
  1105. x->authorization.len = authorization.len;
  1106. x->authorization.s = shm_malloc(x->authorization.len);
  1107. if (!x->authorization.s) {
  1108. LM_ERR("new_auth_vector: error allocating mem\n");
  1109. goto done;
  1110. }
  1111. memcpy(x->authorization.s, authorization.s, authorization.len);
  1112. x->authorization.len = authorization.len;
  1113. break;
  1114. case AUTH_NASS_BUNDLED:
  1115. /* NASS-Bundled */
  1116. x->authenticate.len = 0;
  1117. x->authenticate.s = 0;
  1118. x->authorization.len = authorization.len;
  1119. x->authorization.s = shm_malloc(x->authorization.len);
  1120. if (!x->authorization.s) {
  1121. LM_ERR("new_auth_vector: error allocating mem\n");
  1122. goto done;
  1123. }
  1124. memcpy(x->authorization.s, authorization.s, authorization.len);
  1125. x->authorization.len = authorization.len;
  1126. break;
  1127. default:
  1128. /* all else */
  1129. x->authenticate.len = 0;
  1130. x->authenticate.s = 0;
  1131. }
  1132. x->use_nb = 0;
  1133. x->next = 0;
  1134. x->prev = 0;
  1135. x->status = AUTH_VECTOR_UNUSED;
  1136. x->expires = 0;
  1137. LM_DBG("new auth-vector with ck [%.*s] with status %d\n", x->ck.len, x->ck.s, x->status);
  1138. done:
  1139. return x;
  1140. }
  1141. /**
  1142. * Frees the memory taken by a authentication vector
  1143. * @param av - the vector to be freed
  1144. */
  1145. void free_auth_vector(auth_vector * av) {
  1146. if (av) {
  1147. if (av->authenticate.s) shm_free(av->authenticate.s);
  1148. if (av->authorization.s) shm_free(av->authorization.s);
  1149. if (av->ck.s) shm_free(av->ck.s);
  1150. if (av->ik.s) shm_free(av->ik.s);
  1151. shm_free(av);
  1152. }
  1153. }
  1154. /**
  1155. * Creates a new Authorization Userdata structure.
  1156. * @param private_identity - the private identity to attach to
  1157. * @param public_identity - the public identity to attach to
  1158. * @returns the new auth_userdata* on success or NULL on error
  1159. */
  1160. auth_userdata * new_auth_userdata(str private_identity, str public_identity) {
  1161. auth_userdata *x = 0;
  1162. x = shm_malloc(sizeof (auth_userdata));
  1163. if (!x) {
  1164. LM_ERR("error allocating mem\n");
  1165. goto done;
  1166. }
  1167. x->private_identity.len = private_identity.len;
  1168. x->private_identity.s = shm_malloc(private_identity.len);
  1169. if (!x) {
  1170. LM_ERR("error allocating mem\n");
  1171. goto done;
  1172. }
  1173. memcpy(x->private_identity.s, private_identity.s, private_identity.len);
  1174. x->public_identity.len = public_identity.len;
  1175. x->public_identity.s = shm_malloc(public_identity.len);
  1176. if (!x) {
  1177. LM_ERR("error allocating mem\n");
  1178. goto done;
  1179. }
  1180. memcpy(x->public_identity.s, public_identity.s, public_identity.len);
  1181. x->head = 0;
  1182. x->tail = 0;
  1183. x->next = 0;
  1184. x->prev = 0;
  1185. done:
  1186. return x;
  1187. }
  1188. /**
  1189. * Deallocates the auth_userdata.
  1190. * @param aud - the auth_userdata to be deallocated
  1191. */
  1192. void free_auth_userdata(auth_userdata * aud) {
  1193. auth_vector *av, *next;
  1194. if (aud) {
  1195. if (aud->private_identity.s) shm_free(aud->private_identity.s);
  1196. if (aud->public_identity.s) shm_free(aud->public_identity.s);
  1197. av = aud->head;
  1198. while (av) {
  1199. next = av->next;
  1200. free_auth_vector(av);
  1201. av = next;
  1202. }
  1203. shm_free(aud);
  1204. }
  1205. }
  1206. /**
  1207. * Computes a hash based on the private and public identities
  1208. * @param private_identity - the private identity
  1209. * @param public_identity - the public identity
  1210. * @returns the hash % Auth_data->size
  1211. */
  1212. inline unsigned int get_hash_auth(str private_identity, str public_identity) {
  1213. if (av_check_only_impu)
  1214. return core_hash(&public_identity, 0, act_auth_data_hash_size);
  1215. else
  1216. return core_hash(&public_identity, 0, act_auth_data_hash_size);
  1217. /*
  1218. #define h_inc h+=v^(v>>3)
  1219. char* p;
  1220. register unsigned v;
  1221. register unsigned h;
  1222. h = 0;
  1223. for (p = private_identity.s; p <= (private_identity.s + private_identity.len - 4); p += 4) {
  1224. v = (*p << 24)+(p[1] << 16)+(p[2] << 8) + p[3];
  1225. h_inc;
  1226. }
  1227. v = 0;
  1228. for (; p < (private_identity.s + private_identity.len); p++) {
  1229. v <<= 8;
  1230. v += *p;
  1231. }
  1232. h_inc;
  1233. for (p = public_identity.s; p <= (public_identity.s + public_identity.len - 4); p += 4) {
  1234. v = (*p << 24)+(p[1] << 16)+(p[2] << 8) + p[3];
  1235. h_inc;
  1236. }
  1237. v = 0;
  1238. for (; p < (public_identity.s + public_identity.len); p++) {
  1239. v <<= 8;
  1240. v += *p;
  1241. }
  1242. h = ((h)+(h >> 11))+((h >> 13)+(h >> 23));
  1243. return (h) % auth_data_hash_size;
  1244. #undef h_inc
  1245. */
  1246. }
  1247. /**
  1248. * Retrieve the auth_userdata for a user.
  1249. * \note you will return with lock on the hash slot, so release it!
  1250. * @param private_identity - the private identity
  1251. * @param public_identity - the public identity
  1252. * @returns the auth_userdata* found or newly created on success, NULL on error
  1253. */
  1254. auth_userdata * get_auth_userdata(str private_identity, str public_identity) {
  1255. unsigned int hash = 0;
  1256. auth_userdata *aud = 0;
  1257. hash = get_hash_auth(private_identity, public_identity);
  1258. auth_data_lock(hash);
  1259. aud = auth_data[hash].head;
  1260. if (av_check_only_impu)
  1261. LM_DBG("Searching auth_userdata for IMPU %.*s (Hash %d)\n", public_identity.len, public_identity.s, hash);
  1262. else
  1263. LM_DBG("Searching auth_userdata for IMPU %.*s / IMPI %.*s (Hash %d)\n", public_identity.len, public_identity.s,
  1264. private_identity.len, private_identity.s, hash);
  1265. while (aud) {
  1266. if (av_check_only_impu) {
  1267. if (aud->public_identity.len == public_identity.len &&
  1268. memcmp(aud->public_identity.s, public_identity.s, public_identity.len) == 0) {
  1269. LM_DBG("Found auth_userdata\n");
  1270. return aud;
  1271. }
  1272. } else {
  1273. if (aud->private_identity.len == private_identity.len &&
  1274. aud->public_identity.len == public_identity.len &&
  1275. memcmp(aud->private_identity.s, private_identity.s, private_identity.len) == 0 &&
  1276. memcmp(aud->public_identity.s, public_identity.s, public_identity.len) == 0) {
  1277. LM_DBG("Found auth_userdata\n");
  1278. return aud;
  1279. }
  1280. }
  1281. aud = aud->next;
  1282. }
  1283. /* if we get here, there is no auth_userdata for this user */
  1284. aud = new_auth_userdata(private_identity, public_identity);
  1285. if (!aud) {
  1286. auth_data_unlock(hash);
  1287. return 0;
  1288. }
  1289. aud->prev = auth_data[hash].tail;
  1290. aud->next = 0;
  1291. aud->hash = hash;
  1292. if (!auth_data[hash].head) auth_data[hash].head = aud;
  1293. if (auth_data[hash].tail) auth_data[hash].tail->next = aud;
  1294. auth_data[hash].tail = aud;
  1295. return aud;
  1296. }
  1297. /**
  1298. * Sends a Multimedia-Authentication-Response to retrieve some authentication vectors and maybe synchronize.
  1299. * Must respond with a SIP reply every time it returns 0
  1300. * @param msg - the SIP REGISTER message
  1301. * @param public_identity - the public identity
  1302. * @param private_identity - the private identity
  1303. * @param count - how many vectors to request
  1304. * @param algorithm - which algorithm to request
  1305. * @param nonce - the challenge that will be sent
  1306. * @param auts - the AKA synchronization or empty string if not a synchronization
  1307. * @param server_name - the S-CSCF name to be saved on the HSS
  1308. * @returns 1 on success, 0 on failure
  1309. */
  1310. int multimedia_auth_request(struct sip_msg *msg, str public_identity, str private_identity,
  1311. int count, str auth_scheme, str nonce, str auts, str servername, saved_transaction_t* transaction_data) {
  1312. str authorization = {0, 0};
  1313. int result = -1;
  1314. int is_sync = 0;
  1315. if (auts.len) {
  1316. authorization.s = pkg_malloc(nonce.len * 3 / 4 + auts.len * 3 / 4 + 8);
  1317. if (!authorization.s) {
  1318. LM_ERR("no more pkg mem\n");
  1319. return result;
  1320. }
  1321. authorization.len = base64_to_bin(nonce.s, nonce.len, authorization.s);
  1322. authorization.len = RAND_LEN;
  1323. authorization.len += base64_to_bin(auts.s, auts.len, authorization.s + authorization.len);
  1324. is_sync = 1;
  1325. }
  1326. if (is_sync) {
  1327. drop_auth_userdata(private_identity, public_identity);
  1328. }
  1329. LM_DBG("Sending MAR\n");
  1330. result = cxdx_send_mar(msg, public_identity, private_identity, count, auth_scheme, authorization, servername, transaction_data);
  1331. if (authorization.s) pkg_free(authorization.s);
  1332. return result;
  1333. }
  1334. /**
  1335. * Adds the WWW-Authenticate header for challenge, based on the authentication vector.
  1336. * @param msg - SIP message to add the header to
  1337. * @param realm - the realm
  1338. * @param av - the authentication vector
  1339. * @returns 1 on success, 0 on error
  1340. */
  1341. int pack_challenge(struct sip_msg *msg, str realm, auth_vector *av, int is_proxy_auth) {
  1342. str x = {0, 0};
  1343. char ck[32], ik[32];
  1344. int ck_len, ik_len;
  1345. str *auth_prefix = is_proxy_auth ? &S_Proxy : &S_WWW;
  1346. switch (av->type) {
  1347. case AUTH_AKAV1_MD5:
  1348. case AUTH_AKAV2_MD5:
  1349. /* AKA */
  1350. ck_len = bin_to_base16(av->ck.s, 16, ck);
  1351. ik_len = bin_to_base16(av->ik.s, 16, ik);
  1352. x.len = S_Authorization_AKA.len + auth_prefix->len + realm.len + av->authenticate.len
  1353. + algorithm_types[av->type].len + ck_len + ik_len
  1354. + registration_qop_str.len;
  1355. x.s = pkg_malloc(x.len);
  1356. if (!x.s) {
  1357. LM_ERR("Error allocating %d bytes\n",
  1358. x.len);
  1359. goto error;
  1360. }
  1361. sprintf(x.s, S_Authorization_AKA.s, auth_prefix->len, auth_prefix->s, realm.len, realm.s,
  1362. av->authenticate.len, av->authenticate.s,
  1363. algorithm_types[av->type].len, algorithm_types[av->type].s,
  1364. ck_len, ck, ik_len, ik, registration_qop_str.len,
  1365. registration_qop_str.s);
  1366. x.len = strlen(x.s);
  1367. break;
  1368. case AUTH_HTTP_DIGEST_MD5:
  1369. /* ETSI HTTP_DIGEST MD5 */
  1370. /* this one continues into the next one */
  1371. case AUTH_DIGEST:
  1372. /* Cable-Labs MD5 */
  1373. /* this one continues into the next one */
  1374. case AUTH_SIP_DIGEST:
  1375. /* 3GPP MD5 */
  1376. /* this one continues into the next one */
  1377. case AUTH_MD5:
  1378. /* FOKUS MD5 */
  1379. x.len = S_Authorization_MD5.len + auth_prefix->len + realm.len + av->authenticate.len
  1380. + algorithm_types[av->type].len + registration_qop_str.len;
  1381. x.s = pkg_malloc(x.len);
  1382. if (!x.s) {
  1383. LM_ERR("pack_challenge: Error allocating %d bytes\n", x.len);
  1384. goto error;
  1385. }
  1386. sprintf(x.s, S_Authorization_MD5.s, auth_prefix->len, auth_prefix->s, realm.len, realm.s,
  1387. av->authenticate.len, av->authenticate.s,
  1388. algorithm_types[AUTH_MD5].len, algorithm_types[AUTH_MD5].s,
  1389. registration_qop_str.len, registration_qop_str.s);
  1390. x.len = strlen(x.s);
  1391. break;
  1392. default:
  1393. LM_CRIT("not implemented for algorithm %.*s\n",
  1394. algorithm_types[av->type].len, algorithm_types[av->type].s);
  1395. goto error;
  1396. }
  1397. if (ims_add_header_rpl(msg, &x)) {
  1398. pkg_free(x.s);
  1399. return 1;
  1400. }
  1401. error:
  1402. if (x.s)
  1403. pkg_free(x.s);
  1404. return 0;
  1405. }
  1406. /**
  1407. * Adds the Authentication-Info header for, based on the credentials sent by a successful REGISTER.
  1408. * @param msg - SIP message to add the header to
  1409. * @returns 1 on success, 0 on error
  1410. */
  1411. int add_authinfo_resp_hdr(struct sip_msg *msg, str nextnonce, str qop, HASHHEX rspauth, str cnonce, str nc) {
  1412. str authinfo_hdr;
  1413. static const char authinfo_fmt[] = "Authentication-Info: "
  1414. "nextnonce=\"%.*s\","
  1415. "qop=%.*s,"
  1416. "rspauth=\"%.*s\","
  1417. "cnonce=\"%.*s\","
  1418. "nc=%.*s\r\n";
  1419. authinfo_hdr.len = sizeof (authinfo_fmt) + nextnonce.len + qop.len + HASHHEXLEN + cnonce.len + nc.len - 20 /* format string parameters */ - 1 /* trailing \0 */;
  1420. authinfo_hdr.s = pkg_malloc(authinfo_hdr.len + 1);
  1421. if (!authinfo_hdr.s) {
  1422. LM_ERR("add_authinfo_resp_hdr: Error allocating %d bytes\n", authinfo_hdr.len);
  1423. goto error;
  1424. }
  1425. snprintf(authinfo_hdr.s, authinfo_hdr.len + 1, authinfo_fmt,
  1426. nextnonce.len, nextnonce.s,
  1427. qop.len, qop.s,
  1428. HASHHEXLEN, rspauth,
  1429. cnonce.len, cnonce.s,
  1430. nc.len, nc.s);
  1431. LM_DBG("authinfo hdr built: %.*s", authinfo_hdr.len, authinfo_hdr.s);
  1432. if (ims_add_header_rpl(msg, &authinfo_hdr)) {
  1433. LM_DBG("authinfo hdr added");
  1434. pkg_free(authinfo_hdr.s);
  1435. return 1;
  1436. }
  1437. error:
  1438. if (authinfo_hdr.s) pkg_free(authinfo_hdr.s);
  1439. return 0;
  1440. }
  1441. /**
  1442. * Add an authentication vector to the authentication userdata storage.
  1443. * @param private_identity - the private identity
  1444. * @param public_identity - the public identity
  1445. * @param av - the authentication vector
  1446. * @returns 1 on success or 0 on error
  1447. */
  1448. int add_auth_vector(str private_identity, str public_identity, auth_vector * av) {
  1449. auth_userdata *aud;
  1450. aud = get_auth_userdata(private_identity, public_identity);
  1451. if (!aud) goto error;
  1452. LM_DBG("Adding auth_vector (status %d) for IMPU %.*s / IMPI %.*s (Hash %d)\n", av->status,
  1453. public_identity.len, public_identity.s,
  1454. private_identity.len, private_identity.s, aud->hash);
  1455. av->prev = aud->tail;
  1456. av->next = 0;
  1457. if (!aud->head) aud->head = av;
  1458. if (aud->tail) aud->tail->next = av;
  1459. aud->tail = av;
  1460. auth_data_unlock(aud->hash);
  1461. return 1;
  1462. error:
  1463. return 0;
  1464. }
  1465. /**
  1466. * Declares all auth vectors as useless when we do a synchronization
  1467. * @param private_identity - the private identity
  1468. * @param public_identity - the public identity
  1469. * @returns 1 on sucess, 0 on error
  1470. */
  1471. int drop_auth_userdata(str private_identity, str public_identity) {
  1472. auth_userdata *aud;
  1473. auth_vector *av;
  1474. aud = get_auth_userdata(private_identity, public_identity);
  1475. if (!aud) goto error;
  1476. av = aud->head;
  1477. while (av) {
  1478. LM_DBG("dropping auth vector that was in status %d\n", av->status);
  1479. av->status = AUTH_VECTOR_USELESS;
  1480. av = av->next;
  1481. }
  1482. auth_data_unlock(aud->hash);
  1483. return 1;
  1484. error:
  1485. LM_DBG("no authdata to drop any auth vectors\n");
  1486. if (aud) auth_data_unlock(aud->hash);
  1487. return 0;
  1488. }