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
 * 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"

38
#include <stdio.h>
Pekka Pessi's avatar
Pekka Pessi committed
39 40 41 42 43 44
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <assert.h>

45 46 47 48
#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
49 50 51 52 53

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

  p = s = start;

  /* XXX */

  return start - s;
}
#endif

/* ====================================================================== */
/*
79
 * auth           = ("Authorization" | "Encryption" |
Pekka Pessi's avatar
Pekka Pessi committed
80
 *                   "Proxy-Authenticate" | "Proxy-Authorization" |
81
 *                   "Response-Key" | "WWW-Authenticate") ":"
Pekka Pessi's avatar
Pekka Pessi committed
82 83 84 85 86 87
 *                    scheme 1*SP #auth-param
 * scheme         = token
 * auth-param     = token | token "=" token | token "=" quoted-string
 */

/** Parse security headers. */
88 89 90 91
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
92 93 94 95
{
  msg_auth_t *au = (msg_auth_t *)h;

  au->au_scheme = s;
96

Pekka Pessi's avatar
Pekka Pessi committed
97 98 99 100
  skip_token(&s);
  if (!IS_LWS(*s)) return -1;
  *s++ = '\0';			/* NUL-terminate scheme */

101
  return msg_commalist_d(home, &s, (msg_param_t **)&au->au_params,
Pekka Pessi's avatar
Pekka Pessi committed
102 103 104
			 NULL /* msg_auth_item_scan */);
}

105
issize_t msg_auth_e(char b[], isize_t bsiz, msg_header_t const *h, int f)
Pekka Pessi's avatar
Pekka Pessi committed
106 107 108 109 110 111 112 113 114 115 116
{
  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);
117

Pekka Pessi's avatar
Pekka Pessi committed
118 119 120
  return b - b0;
}

121
/**@internal
Pekka Pessi's avatar
Pekka Pessi committed
122 123 124 125 126 127 128 129 130
 * 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.
 */
131
isize_t msg_auth_dup_xtra(msg_header_t const *h, isize_t offset)
Pekka Pessi's avatar
Pekka Pessi committed
132 133 134
{
  msg_auth_t const *au = h->sh_auth;

135 136
  MSG_PARAMS_SIZE(offset, au->au_params);
  offset += MSG_STRING_SIZE(au->au_scheme);
137

138
  return offset;
Pekka Pessi's avatar
Pekka Pessi committed
139 140 141 142 143 144
}

/**Duplicate one msg_auth_t object. */
char *msg_auth_dup_one(msg_header_t *dst,
		       msg_header_t const *src,
		       char *b,
145
		       isize_t xtra)
Pekka Pessi's avatar
Pekka Pessi committed
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

154
  assert(b <= end); (void)end;
Pekka Pessi's avatar
Pekka Pessi committed
155 156 157

  return b;
}