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
00040 #include <sys/types.h>
00041 #include <stdlib.h>
00042 #include <string.h>
00043 #include <stdio.h>
00044
00045 #include "rfc2617.h"
00046 #include "../../md5global.h"
00047 #include "../../md5.h"
00048
00049
00050 static char hexa_chars[17]="0123456789abcdef";
00051
00052 inline void cvt_hex(HASH _b, HASHHEX _h)
00053 {
00054 unsigned short i;
00055 for (i = 0; i < HASHLEN; i++) {
00056 _h[i * 2] = hexa_chars[(_b[i] >> 4) & 0xf];
00057 _h[i * 2 + 1] = hexa_chars[_b[i] & 0xf];
00058 };
00059 _h[HASHHEXLEN] = '\0';
00060 }
00061
00062
00063
00064
00065
00066 void calc_HA1(ha_alg_t _alg, str* _username, str* _realm, str* _password,
00067 str* _nonce, str* _cnonce, HASHHEX _sess_key)
00068 {
00069 MD5_CTX Md5Ctx;
00070 HASH HA1;
00071 MD5Init(&Md5Ctx);
00072 MD5Update(&Md5Ctx, _username->s, _username->len);
00073 MD5Update(&Md5Ctx, ":", 1);
00074 MD5Update(&Md5Ctx, _realm->s, _realm->len);
00075 MD5Update(&Md5Ctx, ":", 1);
00076 MD5Update(&Md5Ctx, _password->s, _password->len);
00077 MD5Final(HA1, &Md5Ctx);
00078
00079 if (_alg == HA_MD5_SESS) {
00080 MD5Init(&Md5Ctx);
00081 MD5Update(&Md5Ctx, HA1, HASHLEN);
00082 MD5Update(&Md5Ctx, ":", 1);
00083 MD5Update(&Md5Ctx, _nonce->s, _nonce->len);
00084 MD5Update(&Md5Ctx, ":", 1);
00085 MD5Update(&Md5Ctx, _cnonce->s, _cnonce->len);
00086 MD5Final(HA1, &Md5Ctx);
00087 };
00088
00089 cvt_hex(HA1, _sess_key);
00090 }
00091
00092 void calc_H(str *ent, HASHHEX hash)
00093 {
00094 MD5_CTX Md5Ctx;
00095 HASH HA1;
00096 MD5Init(&Md5Ctx);
00097 MD5Update(&Md5Ctx, ent->s, ent->len);
00098 MD5Final(HA1, &Md5Ctx);
00099 cvt_hex(HA1, hash);
00100 }
00101
00102
00103
00104
00105 void calc_response(HASHHEX _ha1,
00106 str* _nonce,
00107 str* _nc,
00108 str* _cnonce,
00109 str* _qop,
00110 int _auth_int,
00111 str* _method,
00112 str* _uri,
00113 HASHHEX _hentity,
00114 HASHHEX _response)
00115 {
00116 MD5_CTX Md5Ctx;
00117 HASH HA2;
00118 HASH RespHash;
00119 HASHHEX HA2Hex;
00120
00121
00122 MD5Init(&Md5Ctx);
00123 MD5Update(&Md5Ctx, _method->s, _method->len);
00124 MD5Update(&Md5Ctx, ":", 1);
00125 MD5Update(&Md5Ctx, _uri->s, _uri->len);
00126
00127 if (_auth_int) {
00128 MD5Update(&Md5Ctx, ":", 1);
00129 MD5Update(&Md5Ctx, _hentity, HASHHEXLEN);
00130 };
00131
00132 MD5Final(HA2, &Md5Ctx);
00133 cvt_hex(HA2, HA2Hex);
00134
00135
00136 MD5Init(&Md5Ctx);
00137 MD5Update(&Md5Ctx, _ha1, HASHHEXLEN);
00138 MD5Update(&Md5Ctx, ":", 1);
00139 MD5Update(&Md5Ctx, _nonce->s, _nonce->len);
00140 MD5Update(&Md5Ctx, ":", 1);
00141
00142 if (_qop->len) {
00143 MD5Update(&Md5Ctx, _nc->s, _nc->len);
00144 MD5Update(&Md5Ctx, ":", 1);
00145 MD5Update(&Md5Ctx, _cnonce->s, _cnonce->len);
00146 MD5Update(&Md5Ctx, ":", 1);
00147 MD5Update(&Md5Ctx, _qop->s, _qop->len);
00148 MD5Update(&Md5Ctx, ":", 1);
00149 };
00150 MD5Update(&Md5Ctx, HA2Hex, HASHHEXLEN);
00151 MD5Final(RespHash, &Md5Ctx);
00152 cvt_hex(RespHash, _response);
00153 }