auth_client.c 22.8 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
 * 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_client.c  Authenticators for SIP client
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * @date Created: Wed Feb 14 18:32:58 2001 ppessi
 */

#include "config.h"

34 35
#include <sofia-sip/su.h>
#include <sofia-sip/su_md5.h>
Pekka Pessi's avatar
Pekka Pessi committed
36

37
#include "sofia-sip/auth_client.h"
38
#include "sofia-sip/auth_client_plugin.h"
Pekka Pessi's avatar
Pekka Pessi committed
39

40
#include <sofia-sip/msg_header.h>
Pekka Pessi's avatar
Pekka Pessi committed
41

42
#include <sofia-sip/auth_digest.h>
43

44 45
#include <sofia-sip/base64.h>
#include <sofia-sip/su_uniqueid.h>
46
#include <sofia-sip/string0.h>
Pekka Pessi's avatar
Pekka Pessi committed
47

48
#include <sofia-sip/su_debug.h>
Pekka Pessi's avatar
Pekka Pessi committed
49

50 51 52
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
Pekka Pessi's avatar
Pekka Pessi committed
53

54
#include <assert.h>
Pekka Pessi's avatar
Pekka Pessi committed
55

56 57 58 59 60
static auth_client_t *ca_create(su_home_t *home, 
				char const *scheme,
				char const *realm);

static void ca_destroy(su_home_t *home, auth_client_t *ca);
Pekka Pessi's avatar
Pekka Pessi committed
61

62
static int ca_challenge(auth_client_t *ca,
Pekka Pessi's avatar
Pekka Pessi committed
63 64
			msg_auth_t const *auth, 
			msg_hclass_t *credential_class,
65
			char const *scheme,
66
			char const *realm);
67

Pekka Pessi's avatar
Pekka Pessi committed
68 69 70 71 72
static int ca_credentials(auth_client_t *ca, 
			  char const *scheme,
			  char const *realm, 
			  char const *user,
			  char const *pass);
73

Pekka Pessi's avatar
Pekka Pessi committed
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
static int ca_clear_credentials(auth_client_t *ca, 
				char const *scheme,
				char const *realm);


/** Initialize authenticators.
 *
 * The function auc_challenge() merges the challenge @a ch to the list of
 * authenticators @a auc_list.  
 *
 * @param auc_list [in/out] list of authenticators to be updated
 * @param home     [in/out] memory home used for allocating authenticators
 * @param ch       [in] challenge to be processed
 * @param crcl     [in] credential class
 * 
 * @retval 1 when challenge was updated
 * @retval 0 when there was no new challenges
 * @retval -1 upon an error
 */
int auc_challenge(auth_client_t **auc_list,
		  su_home_t *home, 
		  msg_auth_t const *ch,
		  msg_hclass_t *crcl)
{
  auth_client_t **cca;
  int retval = 0;

101
  /* Go through each challenge in Authenticate or Proxy-Authenticate headers */
Pekka Pessi's avatar
Pekka Pessi committed
102
  for (; ch; ch = ch->au_next) {
103 104
    char const *scheme = ch->au_scheme;
    char const *realm = msg_header_find_param(ch->au_common, "realm=");
105
    int matched = 0, updated;
Pekka Pessi's avatar
Pekka Pessi committed
106 107 108 109

    if (!scheme || !realm)
      continue;

110
    /* Update matching authenticator */
Pekka Pessi's avatar
Pekka Pessi committed
111
    for (cca = auc_list; (*cca); cca = &(*cca)->ca_next) {
112 113
      updated = ca_challenge((*cca), ch, crcl, scheme, realm);
      if (updated < 0)
114 115
	return -1;
      if (updated == 0)
116 117
	continue;		/* No match, next */
      matched = 1;
118
      if (updated > 1)
119
	retval = 1;		/* Updated authenticator */
Pekka Pessi's avatar
Pekka Pessi committed
120 121
    }

122 123
    if (!matched) {
      /* There was no matching authenticator, create a new one */
124 125
      *cca = ca_create(home, scheme, realm);
      if (ca_challenge((*cca), ch, crcl, scheme, realm) < 0) {
Pekka Pessi's avatar
Pekka Pessi committed
126
	ca_destroy(home, *cca), *cca = NULL;
127
	return -1;
128 129
      }
      retval = 1;		/* Updated authenticator */
Pekka Pessi's avatar
Pekka Pessi committed
130 131 132 133 134 135
    }
  }

  return retval;
}

136 137 138 139 140 141 142
/** Update authentication client. 
 *
 * @retval -1 upon an error
 * @retval 0 when challenge did not match
 * @retval 1 when challenge did match but was not updated
 * @retval 2 when challenge did match and updated client
 */
Pekka Pessi's avatar
Pekka Pessi committed
143 144 145 146
static
int ca_challenge(auth_client_t *ca, 
		 msg_auth_t const *ch,
		 msg_hclass_t *credential_class,
147 148
		 char const *scheme, 
		 char const *realm)
Pekka Pessi's avatar
Pekka Pessi committed
149
{
150
  int stale = 0;
Pekka Pessi's avatar
Pekka Pessi committed
151 152 153 154 155 156

  assert(ca); assert(ch);

  if (!ca || !ch)
    return -1;

157 158 159 160
  if (strcmp(ca->ca_scheme, scheme))
    return 0;
  if (strcmp(ca->ca_realm, realm))
    return 0;
Pekka Pessi's avatar
Pekka Pessi committed
161 162 163

  if (ca->ca_credential_class && 
      ca->ca_credential_class != credential_class)
164
    return 0;
165

166 167 168
  if (ca->ca_auc->auc_challenge)
    stale = ca->ca_auc->auc_challenge(ca, ch);
  if (stale < 0)
Pekka Pessi's avatar
Pekka Pessi committed
169 170
    return -1;

171 172
  if (!ca->ca_credential_class)
    stale = 1, ca->ca_credential_class = credential_class;
Pekka Pessi's avatar
Pekka Pessi committed
173

174
  return stale ? 2 : 1;
Pekka Pessi's avatar
Pekka Pessi committed
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
}

/**Feed authentication data to the authenticator.
 *
 * The function auc_credentials() is used to provide the authenticators in
 * with authentication data (user name, secret).  The authentication data
 * has format as follows:
 *
 * scheme:"realm":user:pass
 *
 * For instance, @c Basic:"nokia-proxy":ppessi:verysecret
 *
 * @todo The authentication data format sucks.
 *
 * @param auc_list [in/out] list of authenticators 
 * @param home     [in/out] memory home used for allocations
 * @param data     [in]     colon-separated authentication data
 * 
 * @retval 0 when successful
 * @retval -1 upon an error
 */
int auc_credentials(auth_client_t **auc_list, su_home_t *home, 
		    char const *data)
{
  int retval = 0, match;
  char *s0, *s;
  char *scheme = NULL, *user = NULL, *pass = NULL, *realm = NULL;

  s0 = s = su_strdup(NULL, data);

  /* Parse authentication data */
  /* Data is string like "Basic:\"agni\":user1:secret" */
  if (s && (s = strchr(scheme = s, ':')))
    *s++ = 0;
  if (s && (s = strchr(realm = s, ':')))
    *s++ = 0;
  if (s && (s = strchr(user = s, ':')))
    *s++ = 0;
  if (s && (s = strchr(pass = s, ':')))
    *s++ = 0;

  if (scheme && realm && user && pass) {
    for (; *auc_list; auc_list = &(*auc_list)->ca_next) {
      match = ca_credentials(*auc_list, scheme, realm, user, pass);
      if (match < 0) {
	retval = -1;
	break;
      }
      if (match) 
	retval++;
    }
  }

  su_free(NULL, s0);

  return retval;
}

/**Feed authentication data to the authenticator.
 *
 * The function auc_credentials() is used to provide the authenticators in
 * with authentication tuple (scheme, realm, user name, secret).  
 *
 * scheme:"realm":user:pass
 *
 * @todo The authentication data format sucks.
 *
 * @param auc_list [in/out] list of authenticators 
 * @param scheme   [in]     scheme to use (NULL, if any)
 * @param realm    [in]     realm to use (NULL, if any)
 * @param user     [in]     username 
 * @param pass     [in]     password
 * 
 * @retval number of matching clients
 * @retval 0 when no matching client was found
 * @retval -1 upon an error
 */
int auc_all_credentials(auth_client_t **auc_list, 
			char const *scheme,
			char const *realm, 
			char const *user,
			char const *pass)
{
  int retval = 0, match;

#if HAVE_SC_CRED_H
  /* XXX: add */
#endif

  if (user && pass) {
    for (; *auc_list; auc_list = &(*auc_list)->ca_next) {
      match = ca_credentials(*auc_list, scheme, realm, user, pass);
      if (match < 0)
	return -1;
      if (match) 
	retval++;
    }
  }

  return retval;
}

int ca_credentials(auth_client_t *ca, 
		   char const *scheme,
		   char const *realm, 
		   char const *user,
		   char const *pass)
{
  assert(ca);

  if (!ca || !ca->ca_scheme || !ca->ca_realm)
    return -1;

  if ((scheme != NULL && strcasecmp(scheme, ca->ca_scheme)) ||
      (realm != NULL && strcmp(realm, ca->ca_realm)))
    return -1;

  ca->ca_user = su_strdup(ca->ca_home, user);
  ca->ca_pass = su_strdup(ca->ca_home, pass);

  if (!ca->ca_user || !ca->ca_pass)
    return -1;

  return 1;
}

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

/** Copy authentication data from @a src to @a dst.
 *
 * @retval >0 if credentials were copied
 * @retval 0 if there was no credentials to copy
 * @retval <0 if an error occurred.
 */
int auc_copy_credentials(auth_client_t **dst,
			 auth_client_t const *src)
{
  int retval = 0;

  if (!dst)
    return -1;

  for (;*dst; dst = &(*dst)->ca_next) {
    auth_client_t *d = *dst;
    auth_client_t const *ca;

    for (ca = src; ca; ca = ca->ca_next) {
      char *u, *p;
      if (!ca->ca_user || !ca->ca_pass)
	continue;
      if (!ca->ca_scheme[0] || strcmp(ca->ca_scheme, d->ca_scheme))
	continue;
      if (!ca->ca_realm[0] || strcmp(ca->ca_realm, d->ca_realm))
	continue;

      if (d->ca_user && strcmp(d->ca_user, ca->ca_user) == 0 &&
	  d->ca_pass && strcmp(d->ca_pass, ca->ca_pass) == 0) {
	retval++;
	break;
      }

      u = su_strdup(d->ca_home, ca->ca_user);
      p = su_strdup(d->ca_home, ca->ca_pass);
      if (!u || !p)
	return -1;

      if (d->ca_user) su_free(d->ca_home, (void *)d->ca_user);
      if (d->ca_pass) su_free(d->ca_home, (void *)d->ca_pass);
      d->ca_user = u, d->ca_pass = p;
      retval++;
      break;
    }
  }

  return retval;
}
      	      

Pekka Pessi's avatar
Pekka Pessi committed
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 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
/**Clear authentication data from the authenticator.
 *
 * The function auc_clear_credentials() is used to remove the credentials
 * from the authenticators.
 *
 * @param auc_list [in/out] list of authenticators 
 * @param scheme   [in] scheme (if non-null, remove only matching credentials) 
 * @param realm    [in] realm (if non-null, remove only matching credentials)
 *
 * @retval 0 when successful
 * @retval -1 upon an error
 */
int auc_clear_credentials(auth_client_t **auc_list, 
			 char const *scheme,
			 char const *realm)
{
  int retval = 0;

  for (; *auc_list; auc_list = &(*auc_list)->ca_next) {
    int match = ca_clear_credentials(*auc_list, scheme, realm);
    if (match < 0) {
      retval = -1;
      break;
    }
    if (match) 
      retval++;
  }

  return retval;
}

static
int ca_clear_credentials(auth_client_t *ca, 
			 char const *scheme,
			 char const *realm)
{
  assert(ca);

  if (!ca || !ca->ca_scheme || !ca->ca_realm)
    return -1;

  if ((scheme != NULL && strcasecmp(scheme, ca->ca_scheme)) ||
      (realm != NULL && strcmp(realm, ca->ca_realm)))
    return -1;

  su_free(ca->ca_home, (void *)ca->ca_user), ca->ca_user = NULL;
  su_free(ca->ca_home, (void *)ca->ca_pass), ca->ca_pass = NULL;

  return 1;
}

/**Authorize a request.
 *
 * The function auc_authorization() is used to add correct authentication
 * headers to a request. The authentication headers will contain the
 * credentials generated by the list of authenticators.
 *
 * @param auc_list [in/out] list of authenticators 
 * @param msg      [out]    message to be authenticated
 * @param pub      [out]    headers of the message
 * @param method   [in]     request method
 * @param url      [in]     request URI
 * @param body     [in]     message body (NULL if empty)
 * 
 * @retval 1 when successful
 * @retval 0 when there is not enough credentials
 * @retval -1 upon an error
 */
int auc_authorization(auth_client_t **auc_list, msg_t *msg, msg_pub_t *pub,
		      char const *method, 
		      url_t const *url, 
		      msg_payload_t const *body)
{
  auth_client_t *ca;
  msg_mclass_t const *mc = msg_mclass(msg);

428 429 430 431 432 433
  if (auc_list == NULL || msg == NULL)
    return -1;

  if (pub == NULL)
    pub = msg_object(msg);

Pekka Pessi's avatar
Pekka Pessi committed
434 435
  /* Make sure every challenge has credentials */
  for (ca = *auc_list; ca; ca = ca->ca_next) {
436
    if (!ca->ca_user || !ca->ca_pass || !ca->ca_credential_class)
Pekka Pessi's avatar
Pekka Pessi committed
437 438 439 440 441 442 443 444 445 446 447 448 449
      return 0;
  }

  /* Remove existing credentials */
  for (ca = *auc_list; ca; ca = ca->ca_next) {
    msg_header_t **hh = msg_hclass_offset(mc, pub, ca->ca_credential_class);

    while (hh && *hh)
      msg_header_remove(msg, pub, *hh);
  }

  /* Insert new credentials */
  for (; *auc_list; auc_list = &(*auc_list)->ca_next) {
450 451
    su_home_t *home = msg_home(msg);
    msg_header_t *h = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
452

453
    ca = *auc_list;
Pekka Pessi's avatar
Pekka Pessi committed
454

455 456 457 458 459 460
    if (!ca->ca_auc)
      continue;

    if (ca->ca_auc->auc_authorize(ca, home, method, url, body, &h) < 0
	|| msg_header_insert(msg, pub, h) < 0)
      return -1;
Pekka Pessi's avatar
Pekka Pessi committed
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
  }

  return 1;
}

/**Generate headers authorizing a request.
 *
 * The function auc_authorization_headers() is used to generate
 * authentication headers for a request. The list of authentication headers
 * will contain the credentials generated by the list of authenticators.
 *
 * @param auc_list [in/out] list of authenticators 
 * @param home     [in]     memory home used to allocate headers
 * @param method   [in]     request method
 * @param url      [in]     request URI
 * @param body     [in]     message body (NULL if empty)
 * @param return_headers [out] authorization headers
 * 
 * @retval 1 when successful
 * @retval 0 when there is not enough credentials
 * @retval -1 upon an error
 */
int auc_authorization_headers(auth_client_t **auc_list, 
			      su_home_t *home,
			      char const *method, 
			      url_t const *url, 
			      msg_payload_t const *body,
			      msg_header_t **return_headers)
{
  auth_client_t *ca;

  /* Make sure every challenge has credentials */
  for (ca = *auc_list; ca; ca = ca->ca_next) {
494
    if (!ca->ca_user || !ca->ca_pass || !ca->ca_credential_class)
Pekka Pessi's avatar
Pekka Pessi committed
495 496 497 498 499
      return 0;
  }

  /* Insert new credentials */
  for (; *auc_list; auc_list = &(*auc_list)->ca_next) {
500
    msg_header_t *h = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
501

502 503 504 505
    ca = *auc_list;

    if (!ca->ca_auc)
      continue;
Pekka Pessi's avatar
Pekka Pessi committed
506

507
    if (ca->ca_auc->auc_authorize(ca, home, method, url, body, &h) < 0)
Pekka Pessi's avatar
Pekka Pessi committed
508 509 510 511
      return -1;

    *return_headers = h;

512 513
    while (*return_headers)
      return_headers = &(*return_headers)->sh_next;
Pekka Pessi's avatar
Pekka Pessi committed
514 515 516 517 518
  }

  return 1;
}

519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
/* ---------------------------------------------------------------------- */
/* Basic scheme */

static int auc_basic_authorization(auth_client_t *ca,
				   su_home_t *h,
				   char const *method, 
				   url_t const *url, 
				   msg_payload_t const *body,
				   msg_header_t **);

const auth_client_plugin_t ca_basic_plugin = 
{ 
  sizeof ca_basic_plugin,
  sizeof (auth_client_t),
  "Basic",
  NULL,
  auc_basic_authorization
};

Pekka Pessi's avatar
Pekka Pessi committed
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
/**Create a basic authorization header.
 *
 * The function auc_basic_authorization() creates a basic authorization
 * header from username @a user and password @a pass. The authorization
 * header type is determined by @a hc - it can be sip_authorization_class,
 * sip_proxy_authorization_class, http_authorization_class, or
 * http_proxy_authorization_class, for instance.
 *
 * @param home memory home used to allocate memory for the new header
 * @param hc   header class for the header to be created
 * @param user user name
 * @param pass password
 * 
 * @return
 * The function auc_basic_authorization() returns a pointer to newly created 
 * authorization header, or NULL upon an error.
 */
555 556 557 558 559 560
int auc_basic_authorization(auth_client_t *ca, 
			    su_home_t *home,
			    char const *method, 
			    url_t const *url, 
			    msg_payload_t const *body,
			    msg_header_t **return_headers)
Pekka Pessi's avatar
Pekka Pessi committed
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
{
  char userpass[49];		/* "reasonable" maximum */
  char base64[65];
  msg_hclass_t *hc = ca->ca_credential_class;
  char const *user = ca->ca_user;
  char const *pass = ca->ca_pass;

  userpass[sizeof(userpass) - 1] = 0;
  base64[sizeof(base64) - 1] = 0;
    
  /*
   * Basic authentication consists of username and password separated by
   * colon and then base64 encoded.
   */
  snprintf(userpass, sizeof(userpass) - 1, "%s:%s", user, pass);
  base64_e(base64, sizeof(base64), userpass, strlen(userpass));

578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
  if (!(*return_headers = msg_header_format(home, hc, "Basic %s", base64)))
    return -1;
  return 0;
}

/* ---------------------------------------------------------------------- */
/* Digest scheme */

typedef struct auth_digest_client_s
{
  auth_client_t cda_client;

  int           cda_ncount;
  char const   *cda_cnonce;
  auth_challenge_t cda_ac[1];
} auth_digest_client_t;

static int auc_digest_challenge(auth_client_t *ca, 
				msg_auth_t const *ch);
static int auc_digest_authorization(auth_client_t *ca, 
				    su_home_t *h,
				    char const *method, 
				    url_t const *url, 
				    msg_payload_t const *body,
				    msg_header_t **);

static const auth_client_plugin_t ca_digest_plugin = 
{ 
  sizeof ca_digest_plugin,
  sizeof (auth_digest_client_t),
  "Digest", 
  auc_digest_challenge,
  auc_digest_authorization
};

/** Store a digest authorization challenge.
 */
static int auc_digest_challenge(auth_client_t *ca, msg_auth_t const *ch)
{
  su_home_t *home = ca->ca_home;
  auth_digest_client_t *cda = (auth_digest_client_t *)ca;
  auth_challenge_t ac[1] = {{ sizeof ac }};
  int stale;

  if (auth_digest_challenge_get(home, ac, ch->au_params) < 0)
    goto error;

  /* Check that we can handle the challenge */
  if (!ac->ac_md5 && !ac->ac_md5sess)
    goto error;
  if (ac->ac_qop && !ac->ac_auth && !ac->ac_auth_int)
    goto error;

  stale = ac->ac_stale || str0cmp(ac->ac_nonce, cda->cda_ac->ac_nonce);

  if (ac->ac_qop && (cda->cda_cnonce == NULL || ac->ac_stale)) {
    su_guid_t guid[1];
    char *cnonce;
    if (cda->cda_cnonce != NULL)
      /* Free the old one if we are updating after stale=true */
      su_free(home, (void *)cda->cda_cnonce);
    su_guid_generate(guid);
    cda->cda_cnonce = cnonce = su_alloc(home, BASE64_SIZE(sizeof(guid)) + 1);
    base64_e(cnonce, BASE64_SIZE(sizeof(guid)) + 1, guid, sizeof(guid));
    cda->cda_ncount = 0;
  }

  auth_digest_challenge_free_params(home, cda->cda_ac);

  *cda->cda_ac = *ac;

  return stale ? 2 : 1;

 error:
  auth_digest_challenge_free_params(home, ac);
  return -1;
Pekka Pessi's avatar
Pekka Pessi committed
654 655
}

656

Pekka Pessi's avatar
Pekka Pessi committed
657 658
/**Create a digest authorization header.
 *
659 660 661 662 663 664
 * Creates a digest authorization header from username @a user and password
 * @a pass, client nonce @a cnonce, client nonce count @a nc, request method
 * @a method, request URI @a uri and message body @a data. The authorization
 * header type is determined by @a hc - it can be either
 * sip_authorization_class or sip_proxy_authorization_class, as well as
 * http_authorization_class or http_proxy_authorization_class.
Pekka Pessi's avatar
Pekka Pessi committed
665 666 667 668 669 670 671 672 673 674 675 676 677 678
 *
 * @param home 	  memory home used to allocate memory for the new header
 * @param hc   	  header class for the header to be created
 * @param user 	  user name
 * @param pass 	  password
 * @param ac      challenge structure
 * @param cnonce  client nonce
 * @param nc      client nonce count 
 * @param method  request method
 * @param uri     request uri
 * @param data    message body
 * @param dlen    length of message body
 *
 * @return
679 680
 * Returns a pointer to newly created authorization header, or NULL upon an
 * error.
Pekka Pessi's avatar
Pekka Pessi committed
681
 */
682 683 684 685 686 687
int auc_digest_authorization(auth_client_t *ca, 
			     su_home_t *home,
			     char const *method, 
			     url_t const *url, 
			     msg_payload_t const *body,
			     msg_header_t **return_headers)
Pekka Pessi's avatar
Pekka Pessi committed
688
{
689
  auth_digest_client_t *cda = (auth_digest_client_t *)ca;
Pekka Pessi's avatar
Pekka Pessi committed
690 691 692
  msg_hclass_t *hc = ca->ca_credential_class;
  char const *user = ca->ca_user;
  char const *pass = ca->ca_pass;
693 694 695
  auth_challenge_t const *ac = cda->cda_ac;
  char const *cnonce = cda->cda_cnonce;
  unsigned nc = ++cda->cda_ncount;
Pekka Pessi's avatar
Pekka Pessi committed
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
  char *uri = url_as_string(home, url);
  void const *data = body ? body->pl_data : "";
  int dlen = body ? body->pl_len : 0;

  msg_header_t *h;
  auth_hexmd5_t sessionkey, response;
  auth_response_t ar[1] = {{ 0 }};
  char ncount[17];

  ar->ar_size = sizeof(ar);
  ar->ar_username = user;
  ar->ar_realm = ac->ac_realm;
  ar->ar_nonce = ac->ac_nonce;
  ar->ar_algorithm = NULL;
  ar->ar_md5 = ac->ac_md5;
  ar->ar_md5sess = ac->ac_md5sess;
  ar->ar_opaque = ac->ac_opaque;
  ar->ar_qop = NULL;
  ar->ar_auth = ac->ac_auth;
  ar->ar_auth_int = ac->ac_auth_int;
  ar->ar_uri = uri;

  /* If there is no qop, we MUST NOT include cnonce or nc */
  if (!ar->ar_auth && !ar->ar_auth_int)
    cnonce = NULL;

  if (cnonce) {
723
    snprintf(ncount, sizeof(ncount), "%08x", nc);
Pekka Pessi's avatar
Pekka Pessi committed
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
    ar->ar_cnonce = cnonce;
    ar->ar_nc = ncount;
  }

  auth_digest_sessionkey(ar, sessionkey, pass);
  auth_digest_response(ar, response, sessionkey, method, data, dlen);

  h = msg_header_format(home, hc, 
			"Digest "
			"username=\"%s\", "
			"realm=\"%s\", "
			"nonce=\"%s"
			"%s%s"
			"%s%s"
			"%s%s, "
			"uri=\"%s\", "
			"response=\"%s\""
			"%s%s"
			"%s%s",
			ar->ar_username, 
			ar->ar_realm,
			ar->ar_nonce,
			cnonce ? "\",  cnonce=\"" : "", 
			cnonce ? cnonce : "",
			ar->ar_opaque ? "\",  opaque=\"" : "", 
			ar->ar_opaque ? ar->ar_opaque : "",
			ar->ar_algorithm ? "\", algorithm=" : "",
			ar->ar_algorithm ? ar->ar_algorithm : "",
			ar->ar_uri,
			response,
			ar->ar_auth || ar->ar_auth_int ? ", qop=" : "", 
			ar->ar_auth_int ? "auth-int" : 
			(ar->ar_auth ? "auth" : ""),
			cnonce ? ", nc=" : "", 
			cnonce ? ncount : "");

  su_free(home, uri);
761 762 763 764 765

  if (!h)
    return -1;
  *return_headers = h;
  return 0;
Pekka Pessi's avatar
Pekka Pessi committed
766 767
}

768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851

/* ---------------------------------------------------------------------- */

#define MAX_AUC 20

static auth_client_plugin_t const *ca_plugins[MAX_AUC] = 
{
  &ca_digest_plugin, &ca_basic_plugin, NULL
};

/** Register an authentication client plugin */
int auc_register_plugin(auth_client_plugin_t const *plugin)
{
  int i;

  if (plugin == NULL ||
      plugin->auc_name == NULL ||
      plugin->auc_authorize == NULL)
    return errno = EFAULT, -1;

  if (plugin->auc_size < sizeof (auth_client_t))
    return errno = EINVAL, -1;

  for (i = 0; i < MAX_AUC; i++) {
    if (ca_plugins[i] == NULL || 
	strcmp(plugin->auc_name, ca_plugins[i]->auc_name) == 0) {
      ca_plugins[i] = plugin;
      return 0;
    }
  }

  return errno = ENOMEM, -1;
}

/** Allocate an (possibly extended) auth_client_t structure. */
static
auth_client_t *ca_create(su_home_t *home,
			 char const *scheme,
			 char const *realm)
{
  auth_client_plugin_t const *auc = NULL;
  auth_client_t *ca;
  size_t realmlen;
  int i;

  if (scheme == NULL || realm == NULL)
    return (void)(errno = EFAULT), NULL;

  realmlen = strlen(realm) + 1;

  for (i = 0; i < MAX_AUC; i++) {
    auc = ca_plugins[i];
    if (!auc || strcasecmp(auc->auc_name, scheme) == 0)
      break;
  }

  if (auc) {
    ca = su_home_clone(home, auc->auc_size + realmlen);
    if (!ca)
      return ca;
    ca->ca_auc = auc;
    ca->ca_scheme = auc->auc_name;
    ca->ca_realm = strcpy((char *)ca + auc->auc_size, realm);
  }
  else {
    size_t schemelen = strlen(scheme) + 1;
    size_t size = sizeof (auth_client_t) + schemelen + realmlen;
    ca = su_home_clone(home, size);
    if (!ca)
      return ca;
    ca->ca_scheme = strcpy((char *)(ca + 1), scheme);
    ca->ca_realm = strcpy((char *)(ca + 1) + schemelen, realm);
  }


  return ca;
}

void ca_destroy(su_home_t *home, auth_client_t *ca)
{
  su_free(home, ca);
}


Pekka Pessi's avatar
Pekka Pessi committed
852
#if HAVE_SOFIA_SIP
853
#include <sofia-sip/sip.h>
Pekka Pessi's avatar
Pekka Pessi committed
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870

/**Authorize a SIP request.
 *
 * The function auc_authorize() is used to add correct authentication
 * headers to a SIP request. The authentication headers will contain the
 * credentials generated by the list of authenticators.
 *
 * @param auc_list [in/out] list of authenticators 
 * @param msg      [in/out] message to be authenticated
 * @param sip      [in/out] sip headers of the message
 * 
 * @retval 1 when successful
 * @retval 0 when there is not enough credentials
 * @retval -1 upon an error
 */
int auc_authorize(auth_client_t **auc_list, msg_t *msg, sip_t *sip)
{
871 872 873 874
  sip_request_t *rq = sip ? sip->sip_request : NULL;

  if (!rq)
    return 0;
Pekka Pessi's avatar
Pekka Pessi committed
875 876 877

  return auc_authorization(auc_list, msg, (msg_pub_t *)sip, 
			   rq->rq_method_name, 
878 879 880 881 882
			   /*
			     RFC 3261 defines the protection domain based
			     only on realm, so we do not bother get a
			     correct URI to auth module.
			   */
Pekka Pessi's avatar
Pekka Pessi committed
883 884 885 886
			   rq->rq_url, 
			   sip->sip_payload);
}
#endif