1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
|
/*
* $Id: digest_parser.c,v 1.1.1.1 2005/06/13 16:47:51 bogdan_iancu Exp $
*
* Digest credentials parser
*
* Copyright (C) 2001-2003 FhG Fokus
*
* This file is part of openser, a free SIP server.
*
* openser is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* openser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* History:
* --------
* 2003-03-02: Added parse_domain function (janakj)
*/
#include "digest_parser.h"
#include "../../trim.h" /* trim_leading */
#include <string.h> /* strncasecmp */
#include "param_parser.h" /* Digest parameter name parser */
#include "../../ut.h" /* q_memchr */
#define DIGEST_SCHEME "digest"
#define DIG_LEN 6
#define QOP_AUTH_STR "auth"
#define QOP_AUTH_STR_LEN 4
#define QOP_AUTHINT_STR "auth-int"
#define QOP_AUTHINT_STR_LEN 8
#define ALG_MD5_STR "MD5"
#define ALG_MD5_STR_LEN 3
#define ALG_MD5SESS_STR "MD5-sess"
#define ALG_MD5SESS_STR_LEN 8
/*
* Parse quoted string in a parameter body
* return the string without quotes in _r
* parameter and update _s to point behind the
* closing quote
*/
static inline int parse_quoted(str* _s, str* _r)
{
char* end_quote;
/* The string must have at least
* surrounding quotes
*/
if (_s->len < 2) {
return -1;
}
/* Skip opening quote */
_s->s++;
_s->len--;
/* Find closing quote */
end_quote = q_memchr(_s->s, '\"', _s->len);
/* Not found, return error */
if (!end_quote) {
return -2;
}
/* Let _r point to the string without
* surrounding quotes
*/
_r->s = _s->s;
_r->len = end_quote - _s->s;
/* Update _s parameter to point
* behind the closing quote
*/
_s->len -= (end_quote - _s->s + 1);
_s->s = end_quote + 1;
/* Everything went OK */
return 0;
}
/*
* Parse unquoted token in a parameter body
* let _r point to the token and update _s
* to point right behind the token
*/
static inline int parse_token(str* _s, str* _r)
{
int i;
/* Save the begining of the
* token in _r->s
*/
_r->s = _s->s;
/* Iterate through the
* token body
*/
for(i = 0; i < _s->len; i++) {
/* All these characters
* mark end of the token
*/
switch(_s->s[i]) {
case ' ':
case '\t':
case '\r':
case '\n':
case ',':
/* So if you find
* any of them
* stop iterating
*/
goto out;
}
}
out:
/* Empty token is error */
if (i == 0) {
return -2;
}
/* Save length of the token */
_r->len = i;
/* Update _s parameter so it points
* right behind the end of the token
*/
_s->s = _s->s + i;
_s->len -= i;
/* Everything went OK */
return 0;
}
/*
* Parse a digest parameter
*/
static inline int parse_digest_param(str* _s, dig_cred_t* _c)
{
dig_par_t t;
str* ptr;
str dummy;
/* Get type of the parameter */
if (parse_param_name(_s, &t) < 0) {
return -1;
}
_s->s++; /* skip = */
_s->len--;
/* Find the begining of body */
trim_leading(_s);
if (_s->len == 0) {
return -2;
}
/* Decide in which attribute the
* body content will be stored
*/
switch(t) {
case PAR_USERNAME: ptr = &_c->username.whole; break;
case PAR_REALM: ptr = &_c->realm; break;
case PAR_NONCE: ptr = &_c->nonce; break;
case PAR_URI: ptr = &_c->uri; break;
case PAR_RESPONSE: ptr = &_c->response; break;
case PAR_CNONCE: ptr = &_c->cnonce; break;
case PAR_OPAQUE: ptr = &_c->opaque; break;
case PAR_QOP: ptr = &_c->qop.qop_str; break;
case PAR_NC: ptr = &_c->nc; break;
case PAR_ALGORITHM: ptr = &_c->alg.alg_str; break;
case PAR_OTHER: ptr = &dummy; break;
default: ptr = &dummy; break;
}
/* If the first character is quote, it is
* a quoted string, otherwise it is a token
*/
if (_s->s[0] == '\"') {
if (parse_quoted(_s, ptr) < 0) {
return -3;
}
} else {
if (parse_token(_s, ptr) < 0) {
return -4;
}
}
return 0;
}
/*
* Parse qop parameter body
*/
static inline void parse_qop(struct qp* _q)
{
str s;
s.s = _q->qop_str.s;
s.len = _q->qop_str.len;
trim(&s);
if ((s.len == QOP_AUTH_STR_LEN) &&
!strncasecmp(s.s, QOP_AUTH_STR, QOP_AUTH_STR_LEN)) {
_q->qop_parsed = QOP_AUTH;
} else if ((s.len == QOP_AUTHINT_STR_LEN) &&
!strncasecmp(s.s, QOP_AUTHINT_STR, QOP_AUTHINT_STR_LEN)) {
_q->qop_parsed = QOP_AUTHINT;
} else {
_q->qop_parsed = QOP_OTHER;
}
}
/*
* Parse algorithm parameter body
*/
static inline void parse_algorithm(struct algorithm* _a)
{
str s;
s.s = _a->alg_str.s;
s.len = _a->alg_str.len;
trim(&s);
if ((s.len == ALG_MD5_STR_LEN) &&
!strncasecmp(s.s, ALG_MD5_STR, ALG_MD5_STR_LEN)) {
_a->alg_parsed = ALG_MD5;
} else if ((s.len == ALG_MD5SESS_STR_LEN) &&
!strncasecmp(s.s, ALG_MD5SESS_STR, ALG_MD5SESS_STR_LEN)) {
_a->alg_parsed = ALG_MD5SESS;
} else {
_a->alg_parsed = ALG_OTHER;
}
}
/*
* Parse username for user and domain parts
*/
static inline void parse_username(struct username* _u)
{
char* d;
_u->user = _u->whole;
if (_u->whole.len <= 2) return;
d = q_memchr(_u->whole.s, '@', _u->whole.len);
if (d) {
_u->domain.s = d + 1;
_u->domain.len = _u->whole.len - (d - _u->whole.s) - 1;
_u->user.len = d - _u->user.s;
}
}
/*
* Parse Digest credentials parameter, one by one
*/
static inline int parse_digest_params(str* _s, dig_cred_t* _c)
{
char* comma;
do {
/* Parse the first parameter */
if (parse_digest_param(_s, _c) < 0) {
return -1;
}
/* Try to find the next parameter */
comma = q_memchr(_s->s, ',', _s->len);
if (comma) {
/* Yes, there is another,
* remove any leading white-spaces
* and let _s point to the next
* parameter name
*/
_s->len -= comma - _s->s + 1;
_s->s = comma + 1;
trim_leading(_s);
}
} while(comma); /* Repeat while there are next parameters */
/* Parse QOP body if the parameter was present */
if (_c->qop.qop_str.s != 0) {
parse_qop(&_c->qop);
}
/* Parse algorithm body if the parameter was present */
if (_c->alg.alg_str.s != 0) {
parse_algorithm(&_c->alg);
}
if (_c->username.whole.s != 0) {
parse_username(&_c->username);
}
return 0;
}
/*
* We support Digest authentication only
*
* Returns:
* 0 - if everything is OK
* -1 - Error while parsing
* 1 - Unknown scheme
*/
int parse_digest_cred(str* _s, dig_cred_t* _c)
{
str tmp;
/* Make a temporary copy, we are
* going to modify it
*/
tmp.s = _s->s;
tmp.len = _s->len;
/* Remove any leading spaces, tabs, \r and \n */
trim_leading(&tmp);
/* Check the string length */
if (tmp.len < (DIG_LEN + 1)) return 1; /* Too short, unknown scheme */
/* Now test, if it is digest scheme, since it is the only
* scheme we are able to parse here
*/
if (!strncasecmp(tmp.s, DIGEST_SCHEME, DIG_LEN) &&
((tmp.s[DIG_LEN] == ' ') || /* Test for one of LWS chars */
(tmp.s[DIG_LEN] == '\r') ||
(tmp.s[DIG_LEN] == 'n') ||
(tmp.s[DIG_LEN] == '\t') ||
(tmp.s[DIG_LEN] == ','))) {
/* Scheme is Digest */
tmp.s += DIG_LEN + 1;
tmp.len -= DIG_LEN + 1;
/* Again, skip all white-spaces */
trim_leading(&tmp);
/* And parse digest parameters */
if (parse_digest_params(&tmp, _c) < 0) {
return -2; /* We must not return -1 in this function ! */
} else {
return 0;
}
} else {
return 1; /* Unknown scheme */
}
}
/*
* Initialize a digest credentials structure
*/
void init_dig_cred(dig_cred_t* _c)
{
memset(_c, 0, sizeof(dig_cred_t));
}
|