digest_parser.c 8.6 KB

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