challenge.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * $Id$
  3. *
  4. * Challenge related functions
  5. *
  6. * Copyright (C) 2001-2003 FhG Fokus
  7. *
  8. * This file is part of ser, a free SIP server.
  9. *
  10. * ser is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * For a license to use the ser software under conditions
  16. * other than those described here, or to purchase support for this
  17. * software, please contact iptel.org by e-mail at the following addresses:
  18. * [email protected]
  19. *
  20. * ser is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  28. *
  29. * History:
  30. * --------
  31. * 2003-01-20 snprintf in build_auth_hf replaced with memcpy to avoid
  32. * possible issues with too small buffer
  33. * 2003-01-26 consume_credentials no longer complains about ACK/CANCEL(jiri)
  34. * 2007-10-19 auth extra checks: longer nonces that include selected message
  35. * parts to protect against various reply attacks without keeping
  36. * state (andrei)
  37. * 2008-07-08 nonce-count (nc) support (andrei)
  38. */
  39. #include "../../data_lump.h"
  40. #include "../../mem/mem.h"
  41. #include "../../parser/digest/digest.h"
  42. #include "../../usr_avp.h"
  43. #include "../../ut.h"
  44. #include "auth_mod.h"
  45. #include "challenge.h"
  46. #include "nonce.h"
  47. #include "api.h"
  48. #include "nc.h"
  49. #include "ot_nonce.h"
  50. #define QOP_PARAM_START ", qop=\""
  51. #define QOP_PARAM_START_LEN (sizeof(QOP_PARAM_START)-1)
  52. #define QOP_PARAM_END "\""
  53. #define QOP_PARAM_END_LEN (sizeof(QOP_PARAM_END)-1)
  54. #define STALE_PARAM ", stale=true"
  55. #define STALE_PARAM_LEN (sizeof(STALE_PARAM)-1)
  56. #define DIGEST_REALM ": Digest realm=\""
  57. #define DIGEST_REALM_LEN (sizeof(DIGEST_REALM)-1)
  58. #define DIGEST_NONCE "\", nonce=\""
  59. #define DIGEST_NONCE_LEN (sizeof(DIGEST_NONCE)-1)
  60. #define DIGEST_MD5 ", algorithm=MD5"
  61. #define DIGEST_MD5_LEN (sizeof(DIGEST_MD5)-1)
  62. #define DIGEST_ALGORITHM ", algorithm="
  63. #define DIGEST_ALGORITHM_LEN (sizeof(DIGEST_ALGORITHM)-1)
  64. extern str auth_realm_prefix;
  65. /**
  66. * @brief Strip the beginning of a realm string
  67. *
  68. * Strip the beginning of a realm string, depending on the length of
  69. * the realm_prefix.
  70. * @param _realm realm string
  71. */
  72. void strip_realm(str* _realm)
  73. {
  74. /* no param defined -- return */
  75. if (!auth_realm_prefix.len) return;
  76. /* prefix longer than realm -- return */
  77. if (auth_realm_prefix.len > _realm->len) return;
  78. /* match ? -- if so, shorten realm -*/
  79. if (memcmp(auth_realm_prefix.s, _realm->s, auth_realm_prefix.len) == 0) {
  80. _realm->s += auth_realm_prefix.len;
  81. _realm->len -= auth_realm_prefix.len;
  82. }
  83. return;
  84. }
  85. /**
  86. * Create and return {WWW,Proxy}-Authenticate header field
  87. * @param nonce nonce value
  88. * @param algorithm algorithm value
  89. * @param qop qop value
  90. * @return -1 on error, 0 on success
  91. *
  92. * The result is stored in param ahf.
  93. * If nonce is not null that it is used, instead of call calc_nonce.
  94. * If algorithm is not null that it is used irrespective of _PRINT_MD5
  95. *
  96. * Major usage of nonce and algorithm params is AKA authentication.
  97. */
  98. int get_challenge_hf(struct sip_msg* msg, int stale, str* realm,
  99. str* nonce, str* algorithm, struct qp* qop, int hftype, str *ahf)
  100. {
  101. char *p;
  102. str* hfn, hf;
  103. int nonce_len, l, cfg;
  104. int t;
  105. #if defined USE_NC || defined USE_OT_NONCE
  106. unsigned int n_id;
  107. unsigned char pool;
  108. unsigned char pool_flags;
  109. #endif
  110. if(!ahf)
  111. {
  112. LM_ERR("invalid output parameter\n");
  113. return -1;
  114. }
  115. strip_realm(realm);
  116. if (realm) {
  117. DEBUG("build_challenge_hf: realm='%.*s'\n", realm->len, realm->s);
  118. }
  119. if (nonce) {
  120. DEBUG("build_challenge_hf: nonce='%.*s'\n", nonce->len, nonce->s);
  121. }
  122. if (algorithm) {
  123. DEBUG("build_challenge_hf: algorithm='%.*s'\n", algorithm->len,
  124. algorithm->s);
  125. }
  126. if (qop && qop->qop_parsed != QOP_UNSPEC) {
  127. DEBUG("build_challenge_hf: qop='%.*s'\n", qop->qop_str.len,
  128. qop->qop_str.s);
  129. }
  130. if (hftype == HDR_PROXYAUTH_T) {
  131. hfn = &proxy_challenge_header;
  132. } else {
  133. hfn = &www_challenge_header;
  134. }
  135. cfg = get_auth_checks(msg);
  136. nonce_len = get_nonce_len(cfg, nc_enabled || otn_enabled);
  137. hf.len = hfn->len;
  138. if (realm) {
  139. hf.len += DIGEST_REALM_LEN
  140. + realm->len;
  141. }
  142. hf.len += DIGEST_NONCE_LEN;
  143. if (nonce) {
  144. hf.len += nonce->len
  145. + 1; /* '"' */
  146. }
  147. else {
  148. hf.len += nonce_len
  149. + 1; /* '"' */
  150. }
  151. hf.len += ((stale) ? STALE_PARAM_LEN : 0);
  152. if (algorithm) {
  153. hf.len += DIGEST_ALGORITHM_LEN + algorithm->len;
  154. }
  155. else {
  156. hf.len += 0
  157. #ifdef _PRINT_MD5
  158. +DIGEST_MD5_LEN
  159. #endif
  160. ;
  161. }
  162. if (qop && qop->qop_parsed != QOP_UNSPEC) {
  163. hf.len += QOP_PARAM_START_LEN + qop->qop_str.len + QOP_PARAM_END_LEN;
  164. }
  165. hf.len += CRLF_LEN;
  166. p = hf.s = pkg_malloc(hf.len);
  167. if (!hf.s) {
  168. ERR("auth: No memory left (%d bytes)\n", hf.len);
  169. return -1;
  170. }
  171. memcpy(p, hfn->s, hfn->len); p += hfn->len;
  172. if(realm){
  173. memcpy(p, DIGEST_REALM, DIGEST_REALM_LEN); p += DIGEST_REALM_LEN;
  174. memcpy(p, realm->s, realm->len); p += realm->len;
  175. }
  176. memcpy(p, DIGEST_NONCE, DIGEST_NONCE_LEN); p += DIGEST_NONCE_LEN;
  177. if (nonce) {
  178. memcpy(p, nonce->s, nonce->len); p += nonce->len;
  179. }
  180. else {
  181. l=nonce_len;
  182. t=time(0);
  183. #if defined USE_NC || defined USE_OT_NONCE
  184. if (nc_enabled || otn_enabled){
  185. pool=nid_get_pool();
  186. n_id=nid_inc(pool);
  187. pool_flags=0;
  188. #ifdef USE_NC
  189. if (nc_enabled){
  190. nc_new(n_id, pool);
  191. pool_flags|= NF_VALID_NC_ID;
  192. }
  193. #endif
  194. #ifdef USE_OT_NONCE
  195. if (otn_enabled){
  196. otn_new(n_id, pool);
  197. pool_flags|= NF_VALID_OT_ID;
  198. }
  199. #endif
  200. }else{
  201. pool=0;
  202. pool_flags=0;
  203. n_id=0;
  204. }
  205. if (calc_nonce(p, &l, cfg, t, t + nonce_expire, n_id,
  206. pool | pool_flags,
  207. &secret1, &secret2, msg) != 0)
  208. #else /* USE_NC || USE_OT_NONCE*/
  209. if (calc_nonce(p, &l, cfg, t, t + nonce_expire,
  210. &secret1, &secret2, msg) != 0)
  211. #endif /* USE_NC || USE_OT_NONCE */
  212. {
  213. ERR("auth: calc_nonce failed (len %d, needed %d)\n",
  214. nonce_len, l);
  215. pkg_free(hf.s);
  216. return -1;
  217. }
  218. p += l;
  219. }
  220. *p = '"'; p++;
  221. if (qop && qop->qop_parsed != QOP_UNSPEC) {
  222. memcpy(p, QOP_PARAM_START, QOP_PARAM_START_LEN);
  223. p += QOP_PARAM_START_LEN;
  224. memcpy(p, qop->qop_str.s, qop->qop_str.len);
  225. p += qop->qop_str.len;
  226. memcpy(p, QOP_PARAM_END, QOP_PARAM_END_LEN);
  227. p += QOP_PARAM_END_LEN;
  228. }
  229. if (stale) {
  230. memcpy(p, STALE_PARAM, STALE_PARAM_LEN);
  231. p += STALE_PARAM_LEN;
  232. }
  233. if (algorithm) {
  234. memcpy(p, DIGEST_ALGORITHM, DIGEST_ALGORITHM_LEN);
  235. p += DIGEST_ALGORITHM_LEN;
  236. memcpy(p, algorithm->s, algorithm->len);
  237. p += algorithm->len;
  238. }
  239. else {
  240. #ifdef _PRINT_MD5
  241. memcpy(p, DIGEST_MD5, DIGEST_MD5_LEN ); p += DIGEST_MD5_LEN;
  242. #endif
  243. }
  244. memcpy(p, CRLF, CRLF_LEN); p += CRLF_LEN;
  245. hf.len=(int)(p-hf.s); /* fix len, it might be smaller due to a smaller
  246. nonce */
  247. DBG("auth: '%.*s'\n", hf.len, ZSW(hf.s));
  248. *ahf = hf;
  249. return 0;
  250. }
  251. /**
  252. * Create {WWW,Proxy}-Authenticate header field
  253. * @param nonce nonce value
  254. * @param algorithm algorithm value
  255. * @return -1 on error, 0 on success
  256. *
  257. * The result is stored in an attribute.
  258. * If nonce is not null that it is used, instead of call calc_nonce.
  259. * If algorithm is not null that it is used irrespective of _PRINT_MD5
  260. * The value of 'qop' module parameter is used.
  261. *
  262. * Major usage of nonce and algorithm params is AKA authentication.
  263. */
  264. int build_challenge_hf(struct sip_msg* msg, int stale, str* realm,
  265. str* nonce, str* algorithm, int hftype)
  266. {
  267. str hf = {0, 0};
  268. avp_value_t val;
  269. int ret;
  270. ret = get_challenge_hf(msg, stale, realm, nonce, algorithm, &auth_qop,
  271. hftype, &hf);
  272. if(ret < 0)
  273. return ret;
  274. val.s = hf;
  275. if(add_avp(challenge_avpid.flags | AVP_VAL_STR, challenge_avpid.name, val)
  276. < 0) {
  277. ERR("auth: Error while creating attribute with challenge\n");
  278. pkg_free(hf.s);
  279. return -1;
  280. }
  281. pkg_free(hf.s);
  282. return 0;
  283. }