auth_digest.c 9.13 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
8
 * This library is free software; you can redistribute it and/or
Pekka Pessi's avatar
Pekka Pessi committed
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
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

/**@CFILE auth_digest.c
 *
 * Implementation for digest authentication.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * @date Created: Thu Feb 22 12:10:37 2001 ppessi
 */

#include "config.h"

36
#include <sofia-sip/su_md5.h>
37
#include "sofia-sip/auth_common.h"
38
#include "sofia-sip/auth_digest.h"
39
#include "sofia-sip/msg_header.h"
40
#include "sofia-sip/auth_common.h"
Pekka Pessi's avatar
Pekka Pessi committed
41 42 43

#include "iptsec_debug.h"

Youness Alaoui's avatar
Youness Alaoui committed
44
#include <stdio.h>
45 46 47 48 49
#include <string.h>
#include <stdarg.h>
#include <limits.h>
#include <assert.h>

Pekka Pessi's avatar
Pekka Pessi committed
50 51 52 53 54 55 56 57 58 59 60
/**Get digest-challenge parameters.
 *
 * The function digest_challenge_get() searches for the digest authentication
 * parameters in @a params.  The parameters are assigned to the appropriate
 * fields in @a ac structure.
 *
 * @return
 *
 * The function digest_challenge_get() returns number of parameters
 * found, or -1 upon an error.
 */
61
issize_t auth_digest_challenge_get(su_home_t *home,
62
				   auth_challenge_t *ac0,
63
				   char const * const params[])
Pekka Pessi's avatar
Pekka Pessi committed
64
{
65
  ssize_t n;
Pekka Pessi's avatar
Pekka Pessi committed
66 67
  auth_challenge_t ac[1] = {{ 0 }};
  char const *md5 = NULL, *md5sess = NULL, *sha1 = NULL,
68
    *stale = NULL,
Pekka Pessi's avatar
Pekka Pessi committed
69 70 71 72
    *qop_auth = NULL, *qop_auth_int = NULL;

  ac->ac_size = sizeof(ac);

73
  assert(ac0);
74
  assert(ac0->ac_size >= (int) sizeof(*ac));
Pekka Pessi's avatar
Pekka Pessi committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88

  if (ac0 == NULL || params == NULL)
    return -1;

  n = auth_get_params(home, params,
		      "realm=", &ac->ac_realm,
		      "domain=", &ac->ac_domain,
		      "nonce=", &ac->ac_nonce,
		      "opaque=", &ac->ac_opaque,
		      "algorithm=", &ac->ac_algorithm,
		      "qop=", &ac->ac_qop,
		      "algorithm=md5", &md5,
		      "algorithm=md5-sess", &md5sess,
		      "algorithm=sha1", &sha1,
89
		      "stale=true", &stale,
Pekka Pessi's avatar
Pekka Pessi committed
90 91 92 93 94 95
		      "qop=auth", &qop_auth,
		      "qop=auth-int", &qop_auth_int,
		      NULL);
  if (n < 0)
    return n;

96
  ac->ac_stale = stale != NULL;
Pekka Pessi's avatar
Pekka Pessi committed
97 98 99 100 101 102 103 104
  ac->ac_md5 = md5 != NULL || ac->ac_algorithm == NULL;
  ac->ac_md5sess = md5sess != NULL;
  ac->ac_sha1 = sha1 != NULL;
  ac->ac_auth = qop_auth != NULL;
  ac->ac_auth_int = qop_auth_int != NULL;

  auth_struct_copy(ac0, ac, sizeof(ac));

105
  SU_DEBUG_5(("%s(): got "MOD_ZD"\n", "auth_digest_challenge_get", n));
106

Pekka Pessi's avatar
Pekka Pessi committed
107 108 109
  return n;
}

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
/** Free challenge parameters */
void auth_digest_challenge_free_params(su_home_t *home, auth_challenge_t *ac)
{
  if (ac->ac_realm)
    su_free(home, (void *)ac->ac_realm), ac->ac_realm = NULL;
  if (ac->ac_domain)
    su_free(home, (void *)ac->ac_domain), ac->ac_domain = NULL;
  if (ac->ac_nonce)
    su_free(home, (void *)ac->ac_nonce), ac->ac_nonce = NULL;
  if (ac->ac_opaque)
    su_free(home, (void *)ac->ac_opaque), ac->ac_opaque = NULL;
  if (ac->ac_algorithm)
    su_free(home, (void *)ac->ac_algorithm), ac->ac_algorithm = NULL;
  if (ac->ac_qop)
    su_free(home, (void *)ac->ac_qop), ac->ac_qop = NULL;
}

Pekka Pessi's avatar
Pekka Pessi committed
127 128 129 130 131 132 133 134 135 136 137
/**Get digest-response parameters.
 *
 * The function auth_response_get() searches for the digest authentication
 * parameters in @a params.  The parameters are assigned to the appropriate
 * fields in @a ar structure.
 *
 * @return
 *
 * The function auth_response_get() returns number of parameters
 * found, or -1 upon an error.
 */
138
issize_t auth_digest_response_get(su_home_t *home,
139
				  auth_response_t *ar0,
140
				  char const *const params[])
Pekka Pessi's avatar
Pekka Pessi committed
141
{
142
  ssize_t n;
Pekka Pessi's avatar
Pekka Pessi committed
143 144 145 146 147 148
  auth_response_t ar[1] = {{ 0 }};
  char const *md5 = NULL, *md5sess = NULL, *sha1 = NULL,
    *qop_auth = NULL, *qop_auth_int = NULL;

  ar->ar_size = sizeof(ar);

149
  assert(ar0); assert(params); assert(ar0->ar_size >= (int) sizeof(ar));
Pekka Pessi's avatar
Pekka Pessi committed
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

  if (ar0 == NULL || params == NULL)
    return -1;

  n = auth_get_params(home, params,
		      "username=", &ar->ar_username,
		      "realm=", &ar->ar_realm,
		      "nonce=", &ar->ar_nonce,
		      "uri=", &ar->ar_uri,
		      "response=", &ar->ar_response,
		      "algorithm=", &ar->ar_algorithm,
		      "opaque=", &ar->ar_opaque,
		      "cnonce=", &ar->ar_cnonce,
		      "qop=", &ar->ar_qop,
		      "nc=", &ar->ar_nc,
		      "algorithm=md5", &md5,
		      "algorithm=md5-sess", &md5sess,
		      "algorithm=sha1", &sha1,
		      "qop=auth", &qop_auth,
		      "qop=auth-int", &qop_auth_int,
		      NULL);
  if (n < 0)
    return n;

  ar->ar_md5      = md5 != NULL || ar->ar_algorithm == NULL;
  ar->ar_md5sess  = md5sess != NULL;
  ar->ar_sha1     = sha1 != NULL;
  ar->ar_auth     = qop_auth != NULL;
  ar->ar_auth_int = qop_auth_int != NULL;

  auth_struct_copy(ar0, ar, sizeof(ar));

182
  SU_DEBUG_7(("%s: "MOD_ZD"\n", "auth_digest_response_get", n));
Pekka Pessi's avatar
Pekka Pessi committed
183 184 185 186 187 188 189 190 191

  return n;
}

static void unquote_update(su_md5_t md5[1], char const *quoted)
{
  if (!quoted)
    /*xyzzy*/;
  else if (quoted[0] == '"') {
192
    char const *q;
193
    size_t n;
Pekka Pessi's avatar
Pekka Pessi committed
194 195 196 197 198 199 200 201 202 203 204 205 206

    for (q = quoted + 1; *q; q += n + 2) {
      n = strcspn(q, "\"\\");
      su_md5_update(md5, q, n);
      if (q[n] == '"' || q[n] == '\0')
	break;
      su_md5_update(md5, q + n + 1, 1);
    }
  }
  else
    su_md5_strupdate(md5, quoted);
}

207
/** Generate A1 hash for digest authentication.
Pekka Pessi's avatar
Pekka Pessi committed
208
 */
209
int auth_digest_a1(auth_response_t *ar,
Pekka Pessi's avatar
Pekka Pessi committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
		   auth_hexmd5_t ha1,
		   char const *secret)
{
  su_md5_t md5[1];

  /* Calculate A1 */
  su_md5_init(md5);
  su_md5_strupdate(md5, ar->ar_username);
  su_md5_update(md5, ":", 1);
  unquote_update(md5, ar->ar_realm);
  su_md5_update(md5, ":", 1);
  su_md5_strupdate(md5, secret);

  su_md5_hexdigest(md5, ha1);

225
  SU_DEBUG_5(("auth_digest_a1() has A1 = MD5(%s:%s:%s) = %s\n",
Pekka Pessi's avatar
Pekka Pessi committed
226 227 228 229 230
	      ar->ar_username, ar->ar_realm, secret, ha1));

  return 0;
}

231
int auth_digest_a1sess(auth_response_t *ar,
Pekka Pessi's avatar
Pekka Pessi committed
232 233 234 235 236 237 238 239 240 241 242 243 244 245
		       auth_hexmd5_t ha1sess,
		       char const *ha1)
{
  su_md5_t md5[1];

  su_md5_init(md5);
  su_md5_strupdate(md5, ha1);
  su_md5_update(md5, ":", 1);
  unquote_update(md5, ar->ar_nonce);
  su_md5_update(md5, ":", 1);
  unquote_update(md5, ar->ar_cnonce);

  su_md5_hexdigest(md5, ha1sess);

246
  SU_DEBUG_5(("auth_sessionkey has A1' = MD5(%s:%s:%s) = %s\n",
Pekka Pessi's avatar
Pekka Pessi committed
247 248 249 250 251
	      ha1, ar->ar_nonce, ar->ar_cnonce, ha1sess));

  return 0;
}

252
/** Generate MD5 session key for digest authentication.
Pekka Pessi's avatar
Pekka Pessi committed
253
 */
254
int auth_digest_sessionkey(auth_response_t *ar,
Pekka Pessi's avatar
Pekka Pessi committed
255 256 257 258 259 260 261 262 263 264 265
			   auth_hexmd5_t ha1,
			   char const *secret)
{
  if (ar->ar_md5sess)
    ar->ar_algorithm = "MD5-sess";
  else if (ar->ar_md5)
    ar->ar_algorithm = "MD5";
  else
    return -1;

  if (ar->ar_md5sess) {
266
    auth_hexmd5_t base_ha1;
Pekka Pessi's avatar
Pekka Pessi committed
267 268 269 270 271 272 273 274 275
    auth_digest_a1(ar, base_ha1, secret);
    auth_digest_a1sess(ar, ha1, base_ha1);
  } else {
    auth_digest_a1(ar, ha1, secret);
  }

  return 0;
}

276
/** Generate response for digest authentication.
Pekka Pessi's avatar
Pekka Pessi committed
277 278
 *
 */
279
int auth_digest_response(auth_response_t *ar,
Pekka Pessi's avatar
Pekka Pessi committed
280
			 auth_hexmd5_t response,
281
			 auth_hexmd5_t const ha1,
Pekka Pessi's avatar
Pekka Pessi committed
282
			 char const *method_name,
283
			 void const *data, isize_t dlen)
Pekka Pessi's avatar
Pekka Pessi committed
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
{
  su_md5_t md5[1];
  auth_hexmd5_t Hentity, HA2;

  if (ar->ar_auth_int)
    ar->ar_qop = "auth-int";
  else if (ar->ar_auth)
    ar->ar_qop = "auth";
  else
    ar->ar_qop = NULL;

  /* Calculate Hentity */
  if (ar->ar_auth_int) {
    if (data && dlen) {
      su_md5_init(md5);
      su_md5_update(md5, data, dlen);
      su_md5_hexdigest(md5, Hentity);
    } else {
      strcpy(Hentity, "d41d8cd98f00b204e9800998ecf8427e");
    }
  }

  /* Calculate A2 */
  su_md5_init(md5);
  su_md5_strupdate(md5, method_name);
  su_md5_update(md5, ":", 1);
  unquote_update(md5, ar->ar_uri);
  if (ar->ar_auth_int) {
    su_md5_update(md5, ":", 1);
    su_md5_update(md5, Hentity, sizeof(Hentity) - 1);
  }
  su_md5_hexdigest(md5, HA2);

317
  SU_DEBUG_5(("A2 = MD5(%s:%s%s%s)\n", method_name, ar->ar_uri,
Pekka Pessi's avatar
Pekka Pessi committed
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
	      ar->ar_auth_int ? ":" : "", ar->ar_auth_int ? Hentity : ""));

  /* Calculate response */
  su_md5_init(md5);
  su_md5_update(md5, ha1, 32);
  su_md5_update(md5, ":", 1);
  unquote_update(md5, ar->ar_nonce);

  if (ar->ar_auth || ar->ar_auth_int) {
    su_md5_update(md5, ":", 1);
    su_md5_strupdate(md5, ar->ar_nc);
    su_md5_update(md5, ":", 1);
    unquote_update(md5, ar->ar_cnonce);
    su_md5_update(md5, ":", 1);
    su_md5_strupdate(md5, ar->ar_qop);
  }

  su_md5_update(md5, ":", 1);
336
  su_md5_update(md5, HA2, 32);
Pekka Pessi's avatar
Pekka Pessi committed
337 338
  su_md5_hexdigest(md5, response);

339 340 341 342 343 344 345 346
  SU_DEBUG_5(("auth_response: %s = MD5(%s:%s%s%s%s%s%s%s:%s) (qop=%s)\n",
	      response, ha1, ar->ar_nonce,
	      ar->ar_auth ||  ar->ar_auth_int ? ":" : "",
	      ar->ar_auth ||  ar->ar_auth_int ? ar->ar_nc : "",
	      ar->ar_auth ||  ar->ar_auth_int ? ":" : "",
	      ar->ar_auth ||  ar->ar_auth_int ? ar->ar_cnonce : "",
	      ar->ar_auth ||  ar->ar_auth_int ? ":" : "",
	      ar->ar_auth ||  ar->ar_auth_int ? ar->ar_qop : "",
Pekka Pessi's avatar
Pekka Pessi committed
347 348 349 350 351
	      HA2,
	      ar->ar_qop ? ar->ar_qop : "NONE"));

  return 0;
}