digest_parser.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * $Id$
  3. *
  4. * Digest credentials parser
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  28. */
  29. #include "digest_parser.h"
  30. #include "../../trim.h" /* trim_leading */
  31. #include <string.h> /* strncasecmp */
  32. #include "param_parser.h" /* Digest parameter name parser */
  33. #include "../../ut.h" /* q_memchr */
  34. #define DIGEST_SCHEME "digest"
  35. #define DIG_LEN 6
  36. #define QOP_AUTH_STR "auth"
  37. #define QOP_AUTH_STR_LEN 4
  38. #define QOP_AUTHINT_STR "auth-int"
  39. #define QOP_AUTHINT_STR_LEN 8
  40. #define ALG_MD5_STR "MD5"
  41. #define ALG_MD5_STR_LEN 3
  42. #define ALG_MD5SESS_STR "MD5-sess"
  43. #define ALG_MD5SESS_STR_LEN 8
  44. /*
  45. * Parse quoted string in a parameter body
  46. * return the string without quotes in _r
  47. * parameter and update _s to point behind the
  48. * closing quote
  49. */
  50. static inline int parse_quoted(str* _s, str* _r)
  51. {
  52. char* end_quote;
  53. /* The string must have at least
  54. * surrounding quotes
  55. */
  56. if (_s->len < 2) {
  57. return -1;
  58. }
  59. /* Skip opening quote */
  60. _s->s++;
  61. _s->len--;
  62. /* Find closing quote */
  63. end_quote = q_memchr(_s->s, '\"', _s->len);
  64. /* Not found, return error */
  65. if (!end_quote) {
  66. return -2;
  67. }
  68. /* Let _r point to the string without
  69. * surrounding quotes
  70. */
  71. _r->s = _s->s;
  72. _r->len = end_quote - _s->s;
  73. /* Update _s parameter to point
  74. * behind the closing quote
  75. */
  76. _s->len -= (end_quote - _s->s + 1);
  77. _s->s = end_quote + 1;
  78. /* Everything went OK */
  79. return 0;
  80. }
  81. /*
  82. * Parse unquoted token in a parameter body
  83. * let _r point to the token and update _s
  84. * to point right behind the token
  85. */
  86. static inline int parse_token(str* _s, str* _r)
  87. {
  88. int i;
  89. /* Save the begining of the
  90. * token in _r->s
  91. */
  92. _r->s = _s->s;
  93. /* Iterate throught the
  94. * token body
  95. */
  96. for(i = 0; i < _s->len; i++) {
  97. /* All these characters
  98. * mark end of the token
  99. */
  100. switch(_s->s[i]) {
  101. case ' ':
  102. case '\t':
  103. case '\r':
  104. case '\n':
  105. case ',':
  106. /* So if you find
  107. * any of them
  108. * stop iterating
  109. */
  110. goto out;
  111. }
  112. }
  113. out:
  114. /* Empty token is error */
  115. if (i == 0) {
  116. return -2;
  117. }
  118. /* Save length of the token */
  119. _r->len = i;
  120. /* Update _s parameter so it points
  121. * right behind the end of the token
  122. */
  123. _s->s = _s->s + i;
  124. _s->len -= i;
  125. /* Everything went OK */
  126. return 0;
  127. }
  128. /*
  129. * Parse a digest parameter
  130. */
  131. static inline int parse_digest_param(str* _s, dig_cred_t* _c)
  132. {
  133. dig_par_t t;
  134. str* ptr;
  135. str dummy;
  136. /* Get type of the parameter */
  137. if (parse_param_name(_s, &t) < 0) {
  138. return -1;
  139. }
  140. _s->s++; /* skip = */
  141. _s->len--;
  142. /* Find the begining of body */
  143. trim_leading(_s);
  144. if (_s->len == 0) {
  145. return -2;
  146. }
  147. /* Decide in which attribute the
  148. * body content will be stored
  149. */
  150. switch(t) {
  151. case PAR_USERNAME: ptr = &(_c->username); break;
  152. case PAR_REALM: ptr = &(_c->realm); break;
  153. case PAR_NONCE: ptr = &(_c->nonce); break;
  154. case PAR_URI: ptr = &(_c->uri); break;
  155. case PAR_RESPONSE: ptr = &(_c->response); break;
  156. case PAR_CNONCE: ptr = &(_c->cnonce); break;
  157. case PAR_OPAQUE: ptr = &(_c->opaque); break;
  158. case PAR_QOP: ptr = &(_c->qop.qop_str); break;
  159. case PAR_NC: ptr = &(_c->nc); break;
  160. case PAR_ALGORITHM: ptr = &(_c->alg.alg_str); break;
  161. case PAR_OTHER: ptr = &dummy; break;
  162. default: ptr = &dummy; break;
  163. }
  164. /* If the first character is qoute, it is
  165. * a quoted string, otherwise it is a token
  166. */
  167. if (_s->s[0] == '\"') {
  168. if (parse_quoted(_s, ptr) < 0) {
  169. return -3;
  170. }
  171. } else {
  172. if (parse_token(_s, ptr) < 0) {
  173. return -4;
  174. }
  175. }
  176. return 0;
  177. }
  178. /*
  179. * Parse qop parameter body
  180. */
  181. static inline void parse_qop(struct qp* _q)
  182. {
  183. str s;
  184. s.s = _q->qop_str.s;
  185. s.len = _q->qop_str.len;
  186. trim(&s);
  187. if ((s.len == QOP_AUTH_STR_LEN) &&
  188. !strncasecmp(s.s, QOP_AUTH_STR, QOP_AUTH_STR_LEN)) {
  189. _q->qop_parsed = QOP_AUTH;
  190. } else if ((s.len == QOP_AUTHINT_STR_LEN) &&
  191. !strncasecmp(s.s, QOP_AUTHINT_STR, QOP_AUTHINT_STR_LEN)) {
  192. _q->qop_parsed = QOP_AUTHINT;
  193. } else {
  194. _q->qop_parsed = QOP_OTHER;
  195. }
  196. }
  197. /*
  198. * Parse algorithm parameter body
  199. */
  200. static inline void parse_algorithm(struct algorithm* _a)
  201. {
  202. str s;
  203. s.s = _a->alg_str.s;
  204. s.len = _a->alg_str.len;
  205. trim(&s);
  206. if ((s.len == ALG_MD5_STR_LEN) &&
  207. !strncasecmp(s.s, ALG_MD5_STR, ALG_MD5_STR_LEN)) {
  208. _a->alg_parsed = ALG_MD5;
  209. } else if ((s.len == ALG_MD5SESS_STR_LEN) &&
  210. !strncasecmp(s.s, ALG_MD5SESS_STR, ALG_MD5SESS_STR_LEN)) {
  211. _a->alg_parsed = ALG_MD5SESS;
  212. } else {
  213. _a->alg_parsed = ALG_OTHER;
  214. }
  215. }
  216. /*
  217. * Parse Digest credentials parameter, one by one
  218. */
  219. static inline int parse_digest_params(str* _s, dig_cred_t* _c)
  220. {
  221. char* comma;
  222. do {
  223. /* Parse the first parameter */
  224. if (parse_digest_param(_s, _c) < 0) {
  225. return -1;
  226. }
  227. /* Try to find the next parameter */
  228. comma = q_memchr(_s->s, ',', _s->len);
  229. if (comma) {
  230. /* Yes, there is another,
  231. * remove any leading whitespaces
  232. * and let _s point to the next
  233. * parameter name
  234. */
  235. _s->len -= comma - _s->s + 1;
  236. _s->s = comma + 1;
  237. trim_leading(_s);
  238. }
  239. } while(comma); /* Repeat while there are next parameters */
  240. /* Parse QOP body if the parameter was present */
  241. if (_c->qop.qop_str.s != 0) {
  242. parse_qop(&(_c->qop));
  243. }
  244. /* Parse algorithm body if the parameter was present */
  245. if (_c->alg.alg_str.s != 0) {
  246. parse_algorithm(&(_c->alg));
  247. }
  248. return 0;
  249. }
  250. /*
  251. * We support Digest authentication only
  252. *
  253. * Returns:
  254. * 0 - if everything is OK
  255. * -1 - Error while parsing
  256. * 1 - Unknown scheme
  257. */
  258. int parse_digest_cred(str* _s, dig_cred_t* _c)
  259. {
  260. str tmp;
  261. /* Make a temporary copy, we are
  262. * going to modify it
  263. */
  264. tmp.s = _s->s;
  265. tmp.len = _s->len;
  266. /* Remove any leading spaces, tabs, \r and \n */
  267. trim_leading(&tmp);
  268. /* Check the string length */
  269. if (tmp.len < (DIG_LEN + 1)) return 1; /* Too short, unknown scheme */
  270. /* Now test, if it is digest scheme, since it is the only
  271. * scheme we are able to parse here
  272. */
  273. if (!strncasecmp(tmp.s, DIGEST_SCHEME, DIG_LEN) &&
  274. ((tmp.s[DIG_LEN] == ' ') || /* Test for one of LWS chars */
  275. (tmp.s[DIG_LEN] == '\r') ||
  276. (tmp.s[DIG_LEN] == 'n') ||
  277. (tmp.s[DIG_LEN] == '\t') ||
  278. (tmp.s[DIG_LEN] == ','))) {
  279. /* Scheme is Digest */
  280. tmp.s += DIG_LEN + 1;
  281. tmp.len -= DIG_LEN + 1;
  282. /* Again, skip all whitespaces */
  283. trim_leading(&tmp);
  284. /* And parse digest parameters */
  285. if (parse_digest_params(&tmp, _c) < 0) {
  286. return -1;
  287. } else {
  288. return 0;
  289. }
  290. } else {
  291. return 1; /* Unknown scheme */
  292. }
  293. }
  294. /*
  295. * Initialize a digest credentials structure
  296. */
  297. void init_dig_cred(dig_cred_t* _c)
  298. {
  299. memset(_c, 0, sizeof(dig_cred_t));
  300. }