auth_ntlm.c 8.1 KB
Newer Older
Martti Mela's avatar
Martti Mela committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * 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
 *
 */

Pekka Pessi's avatar
Pekka Pessi committed
25
/**@CFILE auth_ntlm.c
Martti Mela's avatar
Martti Mela committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
 *
 * Implementation for digest authentication.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * @date Created: Thu Feb 22 12:10:37 2001 ppessi
 */

#include "config.h"

#include <string.h>
#include <stdarg.h>
#include <assert.h>

#include <sofia-sip/su_md5.h>
#include "sofia-sip/auth_digest.h"
#include "sofia-sip/auth_ntlm.h"

#include "iptsec_debug.h"

46
su_inline int has_token(char const *qstring, char const *token);
Martti Mela's avatar
Martti Mela committed
47 48 49 50 51 52 53 54 55 56 57 58 59


/**Get ntlm-challenge parameters.
 *
 * The function ntlm_challenge_get() searches for the ntlm authentication
 * parameters in @a params.  The parameters are assigned to the appropriate
 * fields in @a ac structure.
 *
 * @return
 *
 * The function ntlm_challenge_get() returns number of parameters
 * found, or -1 upon an error.
 */
60
issize_t auth_ntlm_challenge_get(su_home_t *home,
61
				auth_challenge_t *ac0,
62
				char const * const params[])
Martti Mela's avatar
Martti Mela committed
63
{
64
  ssize_t n;
Martti Mela's avatar
Martti Mela committed
65 66 67 68 69 70
  auth_challenge_t ac[1] = {{ 0 }};
  char const *md5 = NULL, *md5sess = NULL, *sha1 = NULL,
    *qop_auth = NULL, *qop_auth_int = NULL;

  ac->ac_size = sizeof(ac);

71
  assert(ac0);
Martti Mela's avatar
Martti Mela committed
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
  assert(ac0->ac_size >= sizeof(*ac));

  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,
		      "stale=", &ac->ac_stale,
		      "algorithm=", &ac->ac_algorithm,
		      "qop=", &ac->ac_qop,
		      "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;

  if (ac->ac_stale && strcasecmp(ac->ac_stale, "true") != 0)
    ac->ac_stale = NULL;

  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));

  SU_DEBUG_5(("%s(): got %d\n", "auth_ntlm_challenge_get", n));
106

Martti Mela's avatar
Martti Mela committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120
  return n;
}

/**Get ntlm-response parameters.
 *
 * The function auth_response_get() searches for the ntlm 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.
 */
121
issize_t auth_ntlm_response_get(su_home_t *home,
122
				auth_response_t *ar0,
123
				char const *const params[])
Martti Mela's avatar
Martti Mela committed
124
{
125
  ssize_t n;
Martti Mela's avatar
Martti Mela committed
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
  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);

  assert(ar0); assert(params); assert(ar0->ar_size >= sizeof(ar));

  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));

  SU_DEBUG_7(("%s: %d\n", "auth_ntlm_response_get", n));

  return n;
}

#if 0

172
/** Generate A1 hash for digest authentication.
Martti Mela's avatar
Martti Mela committed
173
 */
174
int auth_digest_a1(auth_response_t *ar,
Martti Mela's avatar
Martti Mela committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
		   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);

190
  SU_DEBUG_5(("auth_digest_a1() has A1 = MD5(%s:%s:%s) = %s\n",
Martti Mela's avatar
Martti Mela committed
191 192 193 194 195
	      ar->ar_username, ar->ar_realm, secret, ha1));

  return 0;
}

196
int auth_digest_a1sess(auth_response_t *ar,
Martti Mela's avatar
Martti Mela committed
197 198 199 200 201 202 203 204 205 206 207 208 209 210
		       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);

211
  SU_DEBUG_5(("auth_sessionkey has A1' = MD5(%s:%s:%s) = %s\n",
Martti Mela's avatar
Martti Mela committed
212 213 214 215 216
	      ha1, ar->ar_nonce, ar->ar_cnonce, ha1sess));

  return 0;
}

217
/** Generate MD5 session key for digest authentication.
Martti Mela's avatar
Martti Mela committed
218
 */
219
int auth_digest_sessionkey(auth_response_t *ar,
Martti Mela's avatar
Martti Mela committed
220 221 222 223 224 225 226 227 228 229 230
			   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) {
231
    auth_hexmd5_t base_ha1;
Martti Mela's avatar
Martti Mela committed
232 233 234 235 236 237 238 239 240 241 242 243 244 245
    auth_digest_a1(ar, base_ha1, secret);
    auth_digest_a1sess(ar, ha1, base_ha1);
  } else {
    auth_digest_a1(ar, ha1, secret);
  }

  return 0;
}

#endif /* 0 */


#if 0

246
/** Generate response for digest authentication.
Martti Mela's avatar
Martti Mela committed
247 248
 *
 */
249
int auth_digest_response(auth_response_t *ar,
Martti Mela's avatar
Martti Mela committed
250
			 auth_hexmd5_t response,
251
			 auth_hexmd5_t const ha1,
Martti Mela's avatar
Martti Mela committed
252
			 char const *method_name,
253
			 void const *data, issize_t dlen)
Martti Mela's avatar
Martti Mela committed
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
{
  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);

287
  SU_DEBUG_5(("A2 = MD5(%s:%s%s%s)\n", method_name, ar->ar_uri,
Martti Mela's avatar
Martti Mela committed
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
	      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);
306
  su_md5_update(md5, HA2, 32);
Martti Mela's avatar
Martti Mela committed
307 308
  su_md5_hexdigest(md5, response);

309 310 311 312 313 314 315 316
  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 : "",
Martti Mela's avatar
Martti Mela committed
317 318 319 320 321 322 323
	      HA2,
	      ar->ar_qop ? ar->ar_qop : "NONE"));

  return 0;
}

#endif