digest.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * Digest credentials parser interface
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. *
  6. * This file is part of ser, a free SIP server.
  7. *
  8. * ser is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * For a license to use the ser software under conditions
  14. * other than those described here, or to purchase support for this
  15. * software, please contact iptel.org by e-mail at the following addresses:
  16. * [email protected]
  17. *
  18. * ser is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. */
  27. #include "digest.h"
  28. #include "../../mem/mem.h" /* pkg_malloc */
  29. #include "../../dprint.h" /* Guess what */
  30. #include <stdio.h> /* printf */
  31. #include <string.h> /* strncasecmp */
  32. /*
  33. * Create and initialize a new credentials structure
  34. */
  35. static inline int new_credentials(struct hdr_field* _h)
  36. {
  37. auth_body_t* b;
  38. b = (auth_body_t*)pkg_malloc(sizeof(auth_body_t));
  39. if (b == 0) {
  40. LOG(L_ERR, "parse_credentials(): No memory left\n");
  41. return -1;
  42. }
  43. init_dig_cred(&(b->digest));
  44. b->stale = 0;
  45. b->authorized = 0;
  46. _h->parsed = (void*)b;
  47. return 0;
  48. }
  49. /*
  50. * Parse digest credentials
  51. * Return value -1 means that the function was unable to allocate
  52. * memory and therefore the server should return Internal Server Error,
  53. * not Bad Request in this case !
  54. * Bad Request should be send when return value != -1
  55. */
  56. int parse_credentials(struct hdr_field* _h)
  57. {
  58. int res;
  59. void** ph_parsed;
  60. if (_h->parsed) {
  61. return 0; /* Already parsed */
  62. }
  63. if (new_credentials(_h) < 0) {
  64. LOG(L_ERR, "parse_credentials(): Can't create new credentials\n");
  65. return -1;
  66. }
  67. /* parse_digest_cred must return < -1 on error otherwise we will be
  68. * unable to distinguish if the error was caused by the server or if the
  69. * credentials are broken
  70. */
  71. res = parse_digest_cred(&(_h->body), &(((auth_body_t*)(_h->parsed))->digest));
  72. if (res != 0) {
  73. ph_parsed=&_h->parsed;
  74. free_credentials((auth_body_t**)ph_parsed);
  75. }
  76. return res;
  77. }
  78. /*
  79. * Free all memory
  80. */
  81. void free_credentials(auth_body_t** _b)
  82. {
  83. pkg_free(*_b);
  84. *_b = 0;
  85. }
  86. /*
  87. * Check semantics of a digest credentials structure
  88. * Make sure that all attributes needed to verify response
  89. * string are set or at least have a default value
  90. *
  91. * The returned value is logical OR of all errors encountered
  92. * during the check, see dig_err_t type for more details
  93. */
  94. dig_err_t check_dig_cred(dig_cred_t* _c)
  95. {
  96. dig_err_t res = E_DIG_OK;
  97. /* Username must be present */
  98. if (_c->username.user.s == 0) res |= E_DIG_USERNAME;
  99. /* Realm must be present */
  100. if (_c->realm.s == 0) res |= E_DIG_REALM;
  101. /* Nonce that was used must be specified */
  102. if (_c->nonce.s == 0) res |= E_DIG_NONCE;
  103. /* URI must be specified */
  104. if (_c->uri.s == 0) res |= E_DIG_URI;
  105. /* We cannot check credentials without response */
  106. if (_c->response.s == 0) res |= E_DIG_RESPONSE;
  107. /* If QOP parameter is present, some additional
  108. * requirements must be met
  109. */
  110. if ((_c->qop.qop_parsed == QOP_AUTH) || (_c->qop.qop_parsed == QOP_AUTHINT)) {
  111. /* CNONCE must be specified */
  112. if (_c->cnonce.s == 0) res |= E_DIG_CNONCE;
  113. /* and also nonce count must be specified */
  114. if (_c->nc.s == 0) res |= E_DIG_NC;
  115. }
  116. return res;
  117. }
  118. /*
  119. * Print credential structure content to stdout
  120. * Just for debugging
  121. */
  122. void print_cred(dig_cred_t* _c)
  123. {
  124. printf("===Digest credentials===\n");
  125. if (_c) {
  126. printf("Username\n");
  127. printf("+--whole = \'%.*s\'\n", _c->username.whole.len, _c->username.whole.s);
  128. printf("+--user = \'%.*s\'\n", _c->username.user.len, _c->username.user.s);
  129. printf("\\--domain = \'%.*s\'\n", _c->username.domain.len, _c->username.domain.s);
  130. printf("Realm = \'%.*s\'\n", _c->realm.len, _c->realm.s);
  131. printf("Nonce = \'%.*s\'\n", _c->nonce.len, _c->nonce.s);
  132. printf("URI = \'%.*s\'\n", _c->uri.len, _c->uri.s);
  133. printf("Response = \'%.*s\'\n", _c->response.len, _c->response.s);
  134. printf("Algorithm = \'%.*s\'\n", _c->alg.alg_str.len, _c->alg.alg_str.s);
  135. printf("\\--parsed = ");
  136. switch(_c->alg.alg_parsed) {
  137. case ALG_UNSPEC: printf("ALG_UNSPEC\n"); break;
  138. case ALG_MD5: printf("ALG_MD5\n"); break;
  139. case ALG_MD5SESS: printf("ALG_MD5SESS\n"); break;
  140. case ALG_OTHER: printf("ALG_OTHER\n"); break;
  141. }
  142. printf("Cnonce = \'%.*s\'\n", _c->cnonce.len, _c->cnonce.s);
  143. printf("Opaque = \'%.*s\'\n", _c->opaque.len, _c->opaque.s);
  144. printf("QOP = \'%.*s\'\n", _c->qop.qop_str.len, _c->qop.qop_str.s);
  145. printf("\\--parsed = ");
  146. switch(_c->qop.qop_parsed) {
  147. case QOP_UNSPEC: printf("QOP_UNSPEC\n"); break;
  148. case QOP_AUTH: printf("QOP_AUTH\n"); break;
  149. case QOP_AUTHINT: printf("QOP_AUTHINT\n"); break;
  150. case QOP_OTHER: printf("QOP_OTHER\n"); break;
  151. }
  152. printf("NC = \'%.*s\'\n", _c->nc.len, _c->nc.s);
  153. }
  154. printf("===/Digest credentials===\n");
  155. }
  156. /*
  157. * Mark credentials as selected so functions
  158. * following authorize know which credentials
  159. * to use if the message contained more than
  160. * one
  161. */
  162. int mark_authorized_cred(struct sip_msg* _m, struct hdr_field* _h)
  163. {
  164. struct hdr_field* f;
  165. switch(_h->type) {
  166. case HDR_AUTHORIZATION_T: f = _m->authorization; break;
  167. case HDR_PROXYAUTH_T: f = _m->proxy_auth; break;
  168. default:
  169. LOG(L_ERR, "mark_authorized_cred(): Invalid header field type\n");
  170. return -1;
  171. }
  172. if (!(f->parsed)) {
  173. if (new_credentials(f) < 0) {
  174. LOG(L_ERR, "mark_authorized_cred(): Error in new_credentials\n");
  175. return -1;
  176. }
  177. }
  178. ((auth_body_t*)(f->parsed))->authorized = _h;
  179. return 0;
  180. }
  181. /*
  182. * Get pointer to authorized credentials, if there are no
  183. * authorized credentials, 0 is returned
  184. */
  185. int get_authorized_cred(struct hdr_field* _f, struct hdr_field** _h)
  186. {
  187. if (_f && _f->parsed) {
  188. *_h = ((auth_body_t*)(_f->parsed))->authorized;
  189. } else {
  190. *_h = 0;
  191. }
  192. return 0;
  193. }
  194. /*
  195. * Find credentials with given realm in a SIP message header
  196. */
  197. int find_credentials(struct sip_msg* msg, str* realm,
  198. hdr_types_t hftype, struct hdr_field** hdr)
  199. {
  200. struct hdr_field** hook, *ptr;
  201. hdr_flags_t hdr_flags;
  202. int res;
  203. str* r;
  204. /*
  205. * Determine if we should use WWW-Authorization or
  206. * Proxy-Authorization header fields, this parameter
  207. * is set in www_authorize and proxy_authorize
  208. */
  209. switch(hftype) {
  210. case HDR_AUTHORIZATION_T:
  211. hook = &(msg->authorization);
  212. hdr_flags=HDR_AUTHORIZATION_F;
  213. break;
  214. case HDR_PROXYAUTH_T:
  215. hook = &(msg->proxy_auth);
  216. hdr_flags=HDR_PROXYAUTH_F;
  217. break;
  218. default:
  219. hook = &(msg->authorization);
  220. hdr_flags=HDR_T2F(hftype);
  221. break;
  222. }
  223. /*
  224. * If the credentials haven't been parsed yet, do it now
  225. */
  226. if (*hook == 0) {
  227. /* No credentials parsed yet */
  228. if (parse_headers(msg, hdr_flags, 0) == -1) {
  229. LOG(L_ERR, "auth:find_credentials: Error while parsing headers\n");
  230. return -1;
  231. }
  232. }
  233. ptr = *hook;
  234. /*
  235. * Iterate through the credentials in the message and
  236. * find credentials with given realm
  237. */
  238. while(ptr) {
  239. res = parse_credentials(ptr);
  240. if (res < 0) {
  241. LOG(L_ERR, "auth:find_credentials: Error while parsing credentials\n");
  242. return (res == -1) ? -2 : -3;
  243. } else if (res == 0) {
  244. r = &(((auth_body_t*)(ptr->parsed))->digest.realm);
  245. if (r->len == realm->len) {
  246. if (!strncasecmp(realm->s, r->s, r->len)) {
  247. *hdr = ptr;
  248. return 0;
  249. }
  250. }
  251. }
  252. if (parse_headers(msg, hdr_flags, 1) == -1) {
  253. LOG(L_ERR, "auth:find_credentials: Error while parsing headers\n");
  254. return -4;
  255. } else {
  256. ptr = next_sibling_hdr(ptr);
  257. if (!ptr)
  258. break;
  259. }
  260. }
  261. /*
  262. * Credentials with given realm not found
  263. */
  264. return 1;
  265. }