2
0

api.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * $Id$
  3. *
  4. * Digest Authentication Module
  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. /*
  30. * History:
  31. * --------
  32. * ...
  33. * 2008-07-01 set c->stale in auth_check_hdr_md5 (andrei)
  34. */
  35. #include <string.h>
  36. #include "api.h"
  37. #include "../../dprint.h"
  38. #include "../../parser/digest/digest.h"
  39. #include "../../sr_module.h"
  40. #include "../../ut.h"
  41. #include "auth_mod.h"
  42. #include "nonce.h"
  43. static int auth_check_hdr_md5(struct sip_msg* msg, auth_body_t* auth_body,
  44. auth_result_t* auth_res);
  45. /*
  46. * Purpose of this function is to find credentials with given realm,
  47. * do sanity check, validate credential correctness and determine if
  48. * we should really authenticate (there must be no authentication for
  49. * ACK and CANCEL
  50. * @param hdr output param where the Authorize headerfield will be returned.
  51. * @param check_hdr pointer to the function checking Authorization header field
  52. */
  53. auth_result_t pre_auth(struct sip_msg* msg, str* realm, hdr_types_t hftype,
  54. struct hdr_field** hdr,
  55. check_auth_hdr_t check_auth_hdr)
  56. {
  57. int ret;
  58. auth_body_t* c;
  59. check_auth_hdr_t check_hf;
  60. auth_result_t auth_rv;
  61. /* ACK and CANCEL must be always authenticated, there is
  62. * no way how to challenge ACK and CANCEL cannot be
  63. * challenged because it must have the same CSeq as
  64. * the request to be canceled.
  65. * PRACK is also not authenticated
  66. */
  67. if (msg->REQ_METHOD & (METHOD_ACK|METHOD_CANCEL|METHOD_PRACK))
  68. return AUTHENTICATED;
  69. /* Try to find credentials with corresponding realm
  70. * in the message, parse them and return pointer to
  71. * parsed structure
  72. */
  73. strip_realm(realm);
  74. ret = find_credentials(msg, realm, hftype, hdr);
  75. if (ret < 0) {
  76. LOG(L_ERR, "auth:pre_auth: Error while looking for credentials\n");
  77. return ERROR;
  78. } else if (ret > 0) {
  79. DBG("auth:pre_auth: Credentials with realm '%.*s' not found\n",
  80. realm->len, ZSW(realm->s));
  81. return NO_CREDENTIALS;
  82. }
  83. /* Pointer to the parsed credentials */
  84. c = (auth_body_t*)((*hdr)->parsed);
  85. /* digest headers are in c->digest */
  86. DBG("auth: digest-algo: %.*s parsed value: %d\n",
  87. c->digest.alg.alg_str.len, c->digest.alg.alg_str.s,
  88. c->digest.alg.alg_parsed);
  89. if (mark_authorized_cred(msg, *hdr) < 0) {
  90. LOG(L_ERR, "auth:pre_auth: Error while marking parsed credentials\n");
  91. return ERROR;
  92. }
  93. /* check authorization header field's validity */
  94. if (check_auth_hdr == NULL) {
  95. check_hf = auth_check_hdr_md5;
  96. } else { /* use check function of external authentication module */
  97. check_hf = check_auth_hdr;
  98. }
  99. /* use the right function */
  100. if (!check_hf(msg, c, &auth_rv)) {
  101. return auth_rv;
  102. }
  103. return DO_AUTHENTICATION;
  104. }
  105. /**
  106. * TODO move it to rfc2617.c
  107. *
  108. * @param auth_res return value of authentication. Maybe the it will be not affected.
  109. * @result if authentication should continue (1) or not (0)
  110. *
  111. */
  112. static int auth_check_hdr_md5(struct sip_msg* msg, auth_body_t* auth,
  113. auth_result_t* auth_res)
  114. {
  115. int ret;
  116. /* Check credentials correctness here */
  117. if (check_dig_cred(&auth->digest) != E_DIG_OK) {
  118. LOG(L_ERR, "auth:pre_auth: Credentials are not filled properly\n");
  119. *auth_res = BAD_CREDENTIALS;
  120. return 0;
  121. }
  122. ret = check_nonce(auth, &secret1, &secret2, msg);
  123. if (ret!=0){
  124. if (ret==3 || ret==4){
  125. /* failed auth_extra_checks or stale */
  126. auth->stale=1; /* we mark the nonce as stale
  127. (hack that makes our life much easier) */
  128. *auth_res = STALE_NONCE;
  129. return 0;
  130. } else if (ret==6) {
  131. *auth_res = NONCE_REUSED;
  132. return 0;
  133. } else {
  134. DBG("auth:pre_auth: Invalid nonce value received (ret %d)\n", ret);
  135. *auth_res = NOT_AUTHENTICATED;
  136. return 0;
  137. }
  138. }
  139. return 1;
  140. }
  141. /*
  142. * Purpose of this function is to do post authentication steps like
  143. * marking authorized credentials and so on.
  144. */
  145. auth_result_t post_auth(struct sip_msg* msg, struct hdr_field* hdr)
  146. {
  147. int res = AUTHENTICATED;
  148. auth_body_t* c;
  149. c = (auth_body_t*)((hdr)->parsed);
  150. if (c->stale ) {
  151. if ((msg->REQ_METHOD == METHOD_ACK) ||
  152. (msg->REQ_METHOD == METHOD_CANCEL)) {
  153. /* Method is ACK or CANCEL, we must accept stale
  154. * nonces because there is no way how to challenge
  155. * with new nonce (ACK has no response associated
  156. * and CANCEL must have the same CSeq as the request
  157. * to be canceled)
  158. */
  159. } else {
  160. c->stale = 1;
  161. res = NOT_AUTHENTICATED;
  162. }
  163. }
  164. return res;
  165. }
  166. /*
  167. * Calculate the response and compare with the given response string
  168. * Authorization is successful if this two strings are same
  169. */
  170. int auth_check_response(dig_cred_t* cred, str* method, char* ha1)
  171. {
  172. HASHHEX resp, hent;
  173. /*
  174. * First, we have to verify that the response received has
  175. * the same length as responses created by us
  176. */
  177. if (cred->response.len != 32) {
  178. DBG("check_response: Receive response len != 32\n");
  179. return BAD_CREDENTIALS;
  180. }
  181. /*
  182. * Now, calculate our response from parameters received
  183. * from the user agent
  184. */
  185. calc_response(ha1, &(cred->nonce),
  186. &(cred->nc), &(cred->cnonce),
  187. &(cred->qop.qop_str), cred->qop.qop_parsed == QOP_AUTHINT,
  188. method, &(cred->uri), hent, resp);
  189. DBG("check_response: Our result = \'%s\'\n", resp);
  190. /*
  191. * And simply compare the strings, the user is
  192. * authorized if they match
  193. */
  194. if (!memcmp(resp, cred->response.s, 32)) {
  195. DBG("check_response: Authorization is OK\n");
  196. return AUTHENTICATED;
  197. } else {
  198. DBG("check_response: Authorization failed\n");
  199. return NOT_AUTHENTICATED;
  200. }
  201. }
  202. int bind_auth_s(auth_api_s_t* api)
  203. {
  204. if (!api) {
  205. LOG(L_ERR, "bind_auth: Invalid parameter value\n");
  206. return -1;
  207. }
  208. api->pre_auth = pre_auth;
  209. api->post_auth = post_auth;
  210. api->build_challenge = build_challenge_hf;
  211. api->qop = &auth_qop;
  212. api->calc_HA1 = calc_HA1;
  213. api->calc_response = calc_response;
  214. api->check_response = auth_check_response;
  215. api->auth_challenge = auth_challenge;
  216. api->pv_authenticate = pv_authenticate;
  217. api->consume_credentials = consume_credentials;
  218. return 0;
  219. }