📄 rfc2617.cpp
字号:
/*
* $Id: rfc2617.c,v 1.3 2002/09/19 12:23:53 jku Rel $
*
* Digest response calculation as per RFC2617
*
* Copyright (C) 2001-2003 Fhg Fokus
*
* This file is part of ser, a free SIP server.
*
* ser 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
*
* For a license to use the ser software under conditions
* other than those described here, or to purchase support for this
* software, please contact iptel.org by e-mail at the following addresses:
* info@iptel.org
*
* ser 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
*/
#include "stdafx.h"
#include <stdlib.h>
#include <string.h>
#include "rfc2617.h"
#include "md5global.h"
#include "md5.h"
inline void cvt_hex(HASH _b, HASHHEX _h)
{
unsigned short i;
unsigned char j;
for (i = 0; i < HASHLEN; i++) {
j = (_b[i] >> 4) & 0xf;
if (j <= 9) {
_h[i * 2] = (j + '0');
} else {
_h[i * 2] = (j + 'a' - 10);
}
j = _b[i] & 0xf;
if (j <= 9) {
_h[i * 2 + 1] = (j + '0');
} else {
_h[i * 2 + 1] = (j + 'a' - 10);
}
};
_h[HASHHEXLEN] = '\0';
}
/*
* calculate H(A1) as per spec
*/
void calc_HA1(ha_alg_t _alg, str* _username, str* _realm, str* _password,
str* _nonce, str* _cnonce, HASHHEX _sess_key)
{
MD5_CTX Md5Ctx;
HASH HA1;
MD5Init(&Md5Ctx);
MD5Update(&Md5Ctx, (POINTER)_username->s, _username->len);
MD5Update(&Md5Ctx, (POINTER)":", 1);
MD5Update(&Md5Ctx, (POINTER)_realm->s, _realm->len);
MD5Update(&Md5Ctx, (POINTER)":", 1);
MD5Update(&Md5Ctx, (POINTER)_password->s, _password->len);
MD5Final((POINTER)HA1, &Md5Ctx);
if (_alg == HA_MD5_SESS) {
MD5Init(&Md5Ctx);
MD5Update(&Md5Ctx, (POINTER)HA1, HASHLEN);
MD5Update(&Md5Ctx, (POINTER)":", 1);
MD5Update(&Md5Ctx, (POINTER)_nonce->s, _nonce->len);
MD5Update(&Md5Ctx, (POINTER)":", 1);
MD5Update(&Md5Ctx, (POINTER)_cnonce->s, _cnonce->len);
MD5Final((POINTER)HA1, &Md5Ctx);
};
cvt_hex(HA1, _sess_key);
}
/*
* calculate request-digest/response-digest as per HTTP Digest spec
*/
void calc_response(HASHHEX _ha1, /* H(A1) */
str* _nonce, /* nonce from server */
str* _nc, /* 8 hex digits */
str* _cnonce, /* client nonce */
str* _qop, /* qop-value: "", "auth", "auth-int" */
int _auth_int, /* 1 if auth-int is used */
str* _method, /* method from the request */
str* _uri, /* requested URL */
HASHHEX _hentity, /* H(entity body) if qop="auth-int" */
HASHHEX _response) /* request-digest or response-digest */
{
MD5_CTX Md5Ctx;
HASH HA2;
HASH RespHash;
HASHHEX HA2Hex;
/* calculate H(A2) */
MD5Init(&Md5Ctx);
MD5Update(&Md5Ctx, (POINTER)_method->s, _method->len);
MD5Update(&Md5Ctx, (POINTER)":", 1);
MD5Update(&Md5Ctx, (POINTER)_uri->s, _uri->len);
if (_auth_int && _hentity) {
MD5Update(&Md5Ctx, (POINTER)":", 1);
MD5Update(&Md5Ctx, (POINTER)_hentity, HASHHEXLEN);
};
MD5Final((POINTER)HA2, &Md5Ctx);
cvt_hex(HA2, HA2Hex);
/* calculate response */
MD5Init(&Md5Ctx);
MD5Update(&Md5Ctx, (POINTER)_ha1, HASHHEXLEN);
MD5Update(&Md5Ctx, (POINTER)":", 1);
MD5Update(&Md5Ctx, (POINTER)_nonce->s, _nonce->len);
MD5Update(&Md5Ctx, (POINTER)":", 1);
if (_qop && _nc && _qop->len) {
MD5Update(&Md5Ctx, (POINTER)_nc->s, _nc->len);
MD5Update(&Md5Ctx, (POINTER)":", 1);
MD5Update(&Md5Ctx, (POINTER)_cnonce->s, _cnonce->len);
MD5Update(&Md5Ctx, (POINTER)":", 1);
MD5Update(&Md5Ctx, (POINTER)_qop->s, _qop->len);
MD5Update(&Md5Ctx, (POINTER)":", 1);
};
MD5Update(&Md5Ctx, (POINTER)HA2Hex, HASHHEXLEN);
MD5Final((POINTER)RespHash, &Md5Ctx);
cvt_hex(RespHash, _response);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -