00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <sys/types.h>
00031 #include <stdlib.h>
00032 #include <string.h>
00033
00034 #include "rfc2617.h"
00035 #include "../../md5.h"
00036
00037
00038
00039
00040
00041
00042
00043 inline void cvt_hex(HASH _b, HASHHEX _h)
00044 {
00045 unsigned short i;
00046 unsigned char j;
00047
00048 for (i = 0; i < HASHLEN; i++) {
00049 j = (_b[i] >> 4) & 0xf;
00050 if (j <= 9) {
00051 _h[i * 2] = (j + '0');
00052 } else {
00053 _h[i * 2] = (j + 'a' - 10);
00054 }
00055
00056 j = _b[i] & 0xf;
00057
00058 if (j <= 9) {
00059 _h[i * 2 + 1] = (j + '0');
00060 } else {
00061 _h[i * 2 + 1] = (j + 'a' - 10);
00062 }
00063 };
00064
00065 _h[HASHHEXLEN] = '\0';
00066 }
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079 void calc_HA1(ha_alg_t _alg, str* _username, str* _realm, str* _password,
00080 str* _nonce, str* _cnonce, HASHHEX _sess_key)
00081 {
00082 MD5_CTX Md5Ctx;
00083 HASH HA1;
00084
00085 MD5Init(&Md5Ctx);
00086 MD5Update(&Md5Ctx, _username->s, _username->len);
00087 MD5Update(&Md5Ctx, ":", 1);
00088 MD5Update(&Md5Ctx, _realm->s, _realm->len);
00089 MD5Update(&Md5Ctx, ":", 1);
00090 MD5Update(&Md5Ctx, _password->s, _password->len);
00091 MD5Final(HA1, &Md5Ctx);
00092
00093 if (_alg == HA_MD5_SESS) {
00094 MD5Init(&Md5Ctx);
00095 MD5Update(&Md5Ctx, HA1, HASHLEN);
00096 MD5Update(&Md5Ctx, ":", 1);
00097 MD5Update(&Md5Ctx, _nonce->s, _nonce->len);
00098 MD5Update(&Md5Ctx, ":", 1);
00099 MD5Update(&Md5Ctx, _cnonce->s, _cnonce->len);
00100 MD5Final(HA1, &Md5Ctx);
00101 };
00102
00103 cvt_hex(HA1, _sess_key);
00104 }
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120 void calc_response(HASHHEX _ha1,
00121 str* _nonce,
00122 str* _nc,
00123 str* _cnonce,
00124 str* _qop,
00125 int _auth_int,
00126 str* _method,
00127 str* _uri,
00128 HASHHEX _hentity,
00129 HASHHEX _response)
00130 {
00131 MD5_CTX Md5Ctx;
00132 HASH HA2;
00133 HASH RespHash;
00134 HASHHEX HA2Hex;
00135
00136
00137 MD5Init(&Md5Ctx);
00138 MD5Update(&Md5Ctx, _method->s, _method->len);
00139 MD5Update(&Md5Ctx, ":", 1);
00140 MD5Update(&Md5Ctx, _uri->s, _uri->len);
00141
00142 if (_auth_int) {
00143 MD5Update(&Md5Ctx, ":", 1);
00144 MD5Update(&Md5Ctx, _hentity, HASHHEXLEN);
00145 };
00146
00147 MD5Final(HA2, &Md5Ctx);
00148 cvt_hex(HA2, HA2Hex);
00149
00150
00151 MD5Init(&Md5Ctx);
00152 MD5Update(&Md5Ctx, _ha1, HASHHEXLEN);
00153 MD5Update(&Md5Ctx, ":", 1);
00154 MD5Update(&Md5Ctx, _nonce->s, _nonce->len);
00155 MD5Update(&Md5Ctx, ":", 1);
00156
00157 if (_qop->len) {
00158 MD5Update(&Md5Ctx, _nc->s, _nc->len);
00159 MD5Update(&Md5Ctx, ":", 1);
00160 MD5Update(&Md5Ctx, _cnonce->s, _cnonce->len);
00161 MD5Update(&Md5Ctx, ":", 1);
00162 MD5Update(&Md5Ctx, _qop->s, _qop->len);
00163 MD5Update(&Md5Ctx, ":", 1);
00164 };
00165 MD5Update(&Md5Ctx, HA2Hex, HASHHEXLEN);
00166 MD5Final(RespHash, &Md5Ctx);
00167 cvt_hex(RespHash, _response);
00168 }