msg_auth.c 3.82 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 36 37 38 39 40 41 42 43
 * 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
 *
 */

/**@ingroup msg_parser
 * @CFILE msg_auth.c
 *
 * Functions for handling authentication-related headers
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * @date Created: Tue Jun 13 02:57:51 2000 ppessi
 *
 */

#include "config.h"

#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <assert.h>

44 45 46 47
#include <sofia-sip/msg.h>
#include <sofia-sip/msg_parser.h>
#include <sofia-sip/msg_header.h>
#include <sofia-sip/bnf.h>
Pekka Pessi's avatar
Pekka Pessi committed
48 49 50 51 52

#if 0
/**
 * Scan and compact an authentication parameter.
 *
53
 * Scan an authentication parameter, which has syntax as follows:
Pekka Pessi's avatar
Pekka Pessi committed
54 55 56 57 58 59 60 61
 * @code
 * auth-item = auth-param | base64-string
 * auth-param = token [ "=" (token | quoted-string)]
 * @endcode
 *
 * Parameters:
 * @param s      pointer to string to scan
 *
62
 * @return Number of characters scanned, or zero upon an error.
Pekka Pessi's avatar
Pekka Pessi committed
63
 */
64
static size_t msg_auth_item_scan(char *start)
Pekka Pessi's avatar
Pekka Pessi committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
{
  char *p, *s;

  p = s = start;

  /* XXX */

  return start - s;
}
#endif

/* ====================================================================== */
/*
 * auth           = ("Authorization" | "Encryption" | 
 *                   "Proxy-Authenticate" | "Proxy-Authorization" |
 *                   "Response-Key" | "WWW-Authenticate") ":" 
 *                    scheme 1*SP #auth-param
 * scheme         = token
 * auth-param     = token | token "=" token | token "=" quoted-string
 */

/** Parse security headers. */
87 88 89 90
issize_t msg_auth_d(su_home_t *home,
		    msg_header_t *h,
		    char *s,
		    isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
91 92 93 94 95 96 97 98 99 100 101 102 103
{
  msg_auth_t *au = (msg_auth_t *)h;

  au->au_scheme = s;
  
  skip_token(&s);
  if (!IS_LWS(*s)) return -1;
  *s++ = '\0';			/* NUL-terminate scheme */

  return msg_commalist_d(home, &s, (msg_param_t **)&au->au_params, 
			 NULL /* msg_auth_item_scan */);
}

104
issize_t msg_auth_e(char b[], isize_t bsiz, msg_header_t const *h, int f)
Pekka Pessi's avatar
Pekka Pessi committed
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
{
  msg_auth_t const *au = (msg_auth_t *)h;
  int compact = MSG_IS_COMPACT(f);
  char *b0 = b, *end = b + bsiz;

  MSG_STRING_E(b, end, au->au_scheme);
  if (au->au_params) {
    MSG_CHAR_E(b, end, ' ');
    MSG_COMMALIST_E(b, end, au->au_params, compact);
  }
  MSG_TERM_E(b, end);
  
  return b - b0;
}

/**@internal 
 * Extra size of a msg_auth_t object.
 *
 * This function calculates extra size required by a msg_auth_t object.
 *
 * @param a pointer to a msg_auth_t object
 *
 * @return
 *   Size of strings related to msg_auth_t object.
 */
130
isize_t msg_auth_dup_xtra(msg_header_t const *h, isize_t offset)
Pekka Pessi's avatar
Pekka Pessi committed
131 132 133
{
  msg_auth_t const *au = h->sh_auth;

134 135
  MSG_PARAMS_SIZE(offset, au->au_params);
  offset += MSG_STRING_SIZE(au->au_scheme);
Pekka Pessi's avatar
Pekka Pessi committed
136
    
137
  return offset;
Pekka Pessi's avatar
Pekka Pessi committed
138 139 140 141 142 143
}

/**Duplicate one msg_auth_t object. */
char *msg_auth_dup_one(msg_header_t *dst,
		       msg_header_t const *src,
		       char *b,
144
		       isize_t xtra)
Pekka Pessi's avatar
Pekka Pessi committed
145 146 147 148 149 150 151 152
{
  msg_auth_t *au = dst->sh_auth;
  msg_auth_t const *o = src->sh_auth;
  char *end = b + xtra;

  b = msg_params_dup(&au->au_params, o->au_params, b, xtra);
  MSG_STRING_DUP(b, au->au_scheme, o->au_scheme);
    
153
  assert(b <= end); (void)end;
Pekka Pessi's avatar
Pekka Pessi committed
154 155 156

  return b;
}