jauth.c

来自「libosip2-3版本的osip源代码」· C语言 代码 · 共 492 行 · 第 1/2 页

C
492
字号
/*  eXosip - This is the eXtended osip library.  Copyright (C) 2002, 2003  Aymeric MOIZARD  - jack@atosc.org    eXosip is free software; you can redistribute it and/or modify  it under the terms of the GNU General Public License as published by  the Free Software Foundation; either version 2 of the License, or  (at your option) any later version.    eXosip 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 General Public License for more details.    You should have received a copy of the GNU General Public License  along with this program; if not, write to the Free Software  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA*/#ifdef ENABLE_MPATROL#include <mpatrol.h>#endif#include "eXosip2.h"#include <eXosip2/eXosip.h>#include <osip2/osip_mt.h>#include <osip2/osip_condv.h>/* #include <osip2/global.h> */#include <osipparser2/osip_md5.h>/* TAKEN from rcf2617.txt */#define HASHLEN 16typedef char HASH[HASHLEN];#define HASHHEXLEN 32typedef char HASHHEX[HASHHEXLEN + 1];#define IN#define OUTextern eXosip_t eXosip;/* Private functions */static void CvtHex (IN HASH Bin, OUT HASHHEX Hex);static void DigestCalcHA1 (IN const char *pszAlg, IN const char *pszUserName,                           IN const char *pszRealm,                           IN const char *pszPassword,                           IN const char *pszNonce, IN const char *pszCNonce,                           OUT HASHHEX SessionKey);static void DigestCalcResponse (IN HASHHEX HA1, IN const char *pszNonce,                                IN const char *pszNonceCount,                                IN const char *pszCNonce,                                IN const char *pszQop,                                IN const char *pszMethod,                                IN const char *pszDigestUri,                                IN HASHHEX HEntity, OUT HASHHEX Response);static voidCvtHex (IN HASH Bin, OUT HASHHEX Hex){  unsigned short i;  unsigned char j;  for (i = 0; i < HASHLEN; i++)    {      j = (Bin[i] >> 4) & 0xf;      if (j <= 9)        Hex[i * 2] = (j + '0');      else        Hex[i * 2] = (j + 'a' - 10);      j = Bin[i] & 0xf;      if (j <= 9)        Hex[i * 2 + 1] = (j + '0');      else        Hex[i * 2 + 1] = (j + 'a' - 10);    };  Hex[HASHHEXLEN] = '\0';}/* calculate H(A1) as per spec */static voidDigestCalcHA1 (IN const char *pszAlg,               IN const char *pszUserName,               IN const char *pszRealm,               IN const char *pszPassword,               IN const char *pszNonce,               IN const char *pszCNonce, OUT HASHHEX SessionKey){  MD5_CTX Md5Ctx;  HASH HA1;  MD5Init (&Md5Ctx);  MD5Update (&Md5Ctx, (unsigned char *) pszUserName, strlen (pszUserName));  MD5Update (&Md5Ctx, (unsigned char *) ":", 1);  MD5Update (&Md5Ctx, (unsigned char *) pszRealm, strlen (pszRealm));  MD5Update (&Md5Ctx, (unsigned char *) ":", 1);  MD5Update (&Md5Ctx, (unsigned char *) pszPassword, strlen (pszPassword));  MD5Final ((unsigned char *) HA1, &Md5Ctx);  if ((pszAlg != NULL) && osip_strcasecmp (pszAlg, "md5-sess") == 0)    {      MD5Init (&Md5Ctx);      MD5Update (&Md5Ctx, (unsigned char *) HA1, HASHLEN);      MD5Update (&Md5Ctx, (unsigned char *) ":", 1);      MD5Update (&Md5Ctx, (unsigned char *) pszNonce, strlen (pszNonce));      MD5Update (&Md5Ctx, (unsigned char *) ":", 1);      MD5Update (&Md5Ctx, (unsigned char *) pszCNonce, strlen (pszCNonce));      MD5Final ((unsigned char *) HA1, &Md5Ctx);    }  CvtHex (HA1, SessionKey);}/* calculate request-digest/response-digest as per HTTP Digest spec */static voidDigestCalcResponse (IN HASHHEX HA1,     /* H(A1) */                    IN const char *pszNonce,    /* nonce from server */                    IN const char *pszNonceCount,       /* 8 hex digits */                    IN const char *pszCNonce,   /* client nonce */                    IN const char *pszQop,      /* qop-value: "", "auth", "auth-int" */                    IN const char *pszMethod,   /* method from the request */                    IN const char *pszDigestUri,        /* requested URL */                    IN HASHHEX HEntity, /* H(entity body) if qop="auth-int" */                    OUT HASHHEX Response                    /* request-digest or response-digest */ ){  MD5_CTX Md5Ctx;  HASH HA2;  HASH RespHash;  HASHHEX HA2Hex;  /* calculate H(A2) */  MD5Init (&Md5Ctx);  MD5Update (&Md5Ctx, (unsigned char *) pszMethod, strlen (pszMethod));  MD5Update (&Md5Ctx, (unsigned char *) ":", 1);  MD5Update (&Md5Ctx, (unsigned char *) pszDigestUri, strlen (pszDigestUri));  if (pszQop == NULL)    {      goto auth_withoutqop;    }  else if (0 == strcmp (pszQop, "auth-int"))    {      goto auth_withauth_int;    }  else if (0 == strcmp (pszQop, "auth"))    {      goto auth_withauth;    }auth_withoutqop:  MD5Final ((unsigned char *) HA2, &Md5Ctx);  CvtHex (HA2, HA2Hex);  /* calculate response */  MD5Init (&Md5Ctx);  MD5Update (&Md5Ctx, (unsigned char *) HA1, HASHHEXLEN);  MD5Update (&Md5Ctx, (unsigned char *) ":", 1);  MD5Update (&Md5Ctx, (unsigned char *) pszNonce, strlen (pszNonce));  MD5Update (&Md5Ctx, (unsigned char *) ":", 1);  goto end;auth_withauth_int:  MD5Update (&Md5Ctx, (unsigned char *) ":", 1);  MD5Update (&Md5Ctx, (unsigned char *) HEntity, HASHHEXLEN);auth_withauth:  MD5Final ((unsigned char *) HA2, &Md5Ctx);  CvtHex (HA2, HA2Hex);  /* calculate response */  MD5Init (&Md5Ctx);  MD5Update (&Md5Ctx, (unsigned char *) HA1, HASHHEXLEN);  MD5Update (&Md5Ctx, (unsigned char *) ":", 1);  MD5Update (&Md5Ctx, (unsigned char *) pszNonce, strlen (pszNonce));  MD5Update (&Md5Ctx, (unsigned char *) ":", 1);  MD5Update (&Md5Ctx, (unsigned char *) pszNonceCount, strlen (pszNonceCount));  MD5Update (&Md5Ctx, (unsigned char *) ":", 1);  MD5Update (&Md5Ctx, (unsigned char *) pszCNonce, strlen (pszCNonce));  MD5Update (&Md5Ctx, (unsigned char *) ":", 1);  MD5Update (&Md5Ctx, (unsigned char *) pszQop, strlen (pszQop));  MD5Update (&Md5Ctx, (unsigned char *) ":", 1);end:  MD5Update (&Md5Ctx, (unsigned char *) HA2Hex, HASHHEXLEN);  MD5Final ((unsigned char *) RespHash, &Md5Ctx);  CvtHex (RespHash, Response);}int__eXosip_create_authorization_header (osip_message_t * previous_answer,                                      const char *rquri, const char *username,                                      const char *passwd, const char *ha1,                                      osip_authorization_t ** auth,                                      const char *method){  osip_authorization_t *aut;  osip_www_authenticate_t *wa = NULL;  char *qop=NULL;  osip_message_get_www_authenticate (previous_answer, 0, &wa);  /* make some test */  if (passwd == NULL)    return -1;  if (wa == NULL || wa->auth_type == NULL      || (wa->realm == NULL) || (wa->nonce == NULL))    {      OSIP_TRACE (osip_trace                  (__FILE__, __LINE__, OSIP_ERROR, NULL,                   "www_authenticate header is not acceptable.\n"));      return -1;    }  if (0 != osip_strcasecmp ("Digest", wa->auth_type))    {      OSIP_TRACE (osip_trace                  (__FILE__, __LINE__, OSIP_ERROR, NULL,                   "Authentication method not supported. (Digest only).\n"));      return -1;    }  /* "MD5" is invalid, but some servers use it. */  if (wa->algorithm != NULL && 0 != osip_strcasecmp ("MD5", wa->algorithm)      && 0 != osip_strcasecmp ("\"MD5\"", wa->algorithm))    {      OSIP_TRACE (osip_trace                  (__FILE__, __LINE__, OSIP_ERROR, NULL,                   "Authentication method not supported. (Digest only).\n"));      return -1;    }  if (0 != osip_authorization_init (&aut))    {      OSIP_TRACE (osip_trace                  (__FILE__, __LINE__, OSIP_ERROR, NULL,                   "allocation with authorization_init failed.\n"));      return -1;    }  /* just copy some feilds from response to new request */  osip_authorization_set_auth_type (aut, osip_strdup ("Digest"));  osip_authorization_set_realm (aut,

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?