必威体育Betway必威体育官网
当前位置:首页 > IT技术

http digest认证过程分析及例子

时间:2019-10-01 18:15:40来源:IT技术作者:seo实验室小编阅读:64次「手机版」
 

digest

验证过程:

         

步骤一、客户端向服务器申请数据                        

****************************request******************************

GET /auth HTTP/1.1(\r\n)

Accept: */*(\r\n)

Host: 192.168.1.15(\r\n)

content-Length: 0(\r\n)

(\r\n\r\n)

步骤二、服务器回复说需要验证,并发送了需要的数据(注意这里是Digest,决定了需要使用Digest认证方式,而不是Basic等),这些数据决定了客户端验证需要遵循的算法

****************************response******************************

HTTP/1.1 401 Unauthorized(\r\n)

Connection: Keep-Alive(\r\n)

Content-Length: 0(\r\n)

Date: Wed, 11 Sep 2013 09:35:54 GMT(\r\n)

WWW-Authenticate: Digestrealm="TestDigest",nonce="c7237893-4eca-478e-b016-548f7998933b",algorithm=MD5,qop="auth"(\r\n)

(\r\n\r\n)

步骤三、发送验证信息

步骤二数据包的分析:

状态码401表示该客户端未被授权,需要客户端发送验证信息,注意这时候必须发送WWW-Authenticate头域,否则客户端不知道该依据什么规则来发送验证信息。依据http头域中WWW-Authenticate中的字段。根据“RFC 2617 - HTTP Authentication: Basic and Digest Access Authenti”该文档中,介绍了如何依据服务器返回的数据发送验证信息。

服务器通过规则计算出的结果与客户端发送的response进行比较,相等才认为是合法用户,于是服务器发送200 OK。

依据该文档:digest的计算如下(附录2,生成发送WWW-Authenticate的函数),即上文中说到的response,计算出来的digest通过response字段发送给服务器。

If "qop" is used, the digest is:

  H(H(A1):nonce:nc:cnonce:qop:H(A2))-----其中cnonce是有自己产生的随机字符串(本文后面附录(1)生成随机字符串函数),其他字符串依据下面规则得出

where H is the hash function such as MD5; A1 and A2 will be given later.

If "qop" is not used, the digest is:

H(H(A1):nonce:H(A2))

If the "algorithm" chosen is "MD5" or is unspecified:

   A1 = Username:realm:password

If the "algorithm" chosen is "MD5-sess", then A1 is calculated only once – 

on the first request by the client following receipt of a WWW-Authenticate challenge from the server.

It uses the server nonce from that challenge, and the first client nonce:

A1 = Username:realm:password:nouce:cnonce

If "qop" is "auth" , then

    A2 = Method:URL

If "qop" is "auth-int" for message integrity, then

A2 = Method:URL:H(entity-body)

显然本例中Response如下计算:

H(H(A1):nonce:nc:cnonce:qop:H(A2))

A1 = unq(username-value) ":" unq(realm-value) ":" passwd

A2 = Method ":" digest-uri-value

其中H是md5算法

****************************Request******************************

GET /Auth HTTP/1.1

Accept: */*

Host: 192.168.1.15

authorization: Digest username="LiPing",realm="TestDigest",qop="auth",algorithm="MD5",uri="/Auth",nonce="1f4b9851-bc18-4dee-91ad-683b5adee7ae",nc=00000001,cnonce="L0RGJ52PLai068jYU55G036655qZF6D7",response="9471d8765dc5c88a78829b2e2e6eb7dd"

步骤四、服务器返回OK,否则继续返回401来告诉客户端需要验证

****************************Response******************************

HTTP/1.1 200 OK

Connection: Keep-Alive

Date: Thu, 12 Sep 2013 00:52:40 GMT

关于该验证过程实现的双向验证机制,是通过服务器或是客户端产生的随机字符串,然后经过MD5算法来实现的。该文最后服务器没有返回Authorization头域,这个也可以的,只是不够规范。

附录:

(1)


 
  1. //函数功能:生成随机字符串

  2. //函数参数:生成随机字符串的长度

  3. //返回值:成功返回随机字符串

  4. char *createRandomNum(int N)

  5. {

  6. int flag;

  7. int k=0,j=0;

  8. char *random_str = (char*)malloc(N+1);

  9. random_str[0] = '\0';

  10. //1970到现在的时间sec作为种子

  11. unsigned int seed = (unsigned)time(NULL);

  12. srand(seed);

  13. for(j=0;j<N;j++)

  14. {

  15. unsigned int random_num = rand();

  16. flag = random_num%3;

  17. if(flag == 0)

  18. {

  19. random_str[k++]='0'+random_num%10;

  20. }

  21. else if(flag == 1)

  22. {

  23. random_str[k++]='a'+random_num%26;

  24. }

  25. else if(flag == 2)

  26. {

  27. random_str[k++]='A'+random_num%26;

  28. }

  29. srand(random_num);

  30. }

  31. random_str[k]='\0';

  32. return random_str;

  33. }

附录2


 
  1. //函数功能:获取子串

  2. //函数参数:source目标字符串;start_str开始字符串;end_chr结束字符

  3. //返回值:成功返回该子串,失败返回NULL

  4. char* GetTargetStr(const char*source,char*start_str,char end_chr)

  5. {

  6. char *p_start = NULL;

  7. char *p_end = NULL;

  8. p_start = strstr(source,start_str);

  9. p_start += strlen(start_str);

  10. p_end = strchr(p_start,end_chr);

  11. char *ret = NULL;

  12. if(p_end != NULL)

  13. {

  14. ret = (char*)malloc(p_end - p_start +1);

  15. ret[p_end - p_start] = '\0';

  16. memcpy(ret,p_start,p_end - p_start);

  17. }

  18. return ret;

  19. }

  20. //函数功能:获取WWW_Authenticate认证信息

  21. //函数参数:self通信句柄;HttpRsp服务器响应数据包;HttpRspSize数据包的尺寸;head_len头长度;user登陆用户名;pwd用户密码

  22. //返回值:成功返回OK,失败

  23. //备注:该函数中nc的值,这里客户端不保存服务器发送的nonce,所以每次都是00000001

  24. char *GetClientWWW_Authenticate(const char*response,long responseSize,int head_len\

  25. ,const char*user,const char*pwd)

  26. {

  27. char *realm = GetTargetStr(response,"realm=\"",'\"');

  28. char *nonce = GetTargetStr(response,"nonce=\"",'\"');

  29. char *algorithm = GetTargetStr(response,"algorithm=",',');

  30. char *qop = GetTargetStr(response,"qop=\"",'\"');

  31.  
  32. assert(realm && nonce && algorithm && qop);

  33. //FIXME

  34. char *nc = "00000001";

  35. char *cnonce = createRandomNum(32);//需要生成随机字符串

  36.  
  37. char A1[100] = {0};

  38. sprintf(A1,"%s:%s:%s",user,realm,pwd);

  39. char *md5_A1 = MD5_sign((unsigned char*)A1,strlen(A1));

  40.  
  41. char A2[80] = {0};

  42. sprintf(A2,"GET:/Auth");

  43. char *md5_A2 = MD5_sign((unsigned char*)A2,strlen(A2));

  44.  
  45. char contact[512] = {0};

  46. sprintf(contact,"%s:%s:%s:%s:%s:%s",md5_A1,nonce,nc,cnonce,qop,md5_A2);

  47. FREE_MALLOC(md5_A1);

  48. FREE_MALLOC(md5_A2);

  49.  
  50. char *rsp = MD5_sign((unsigned char*)contact,strlen(contact));

  51. char WWW_Authenticate[256] = {0};

  52. char*format = "Digest username=\"%s\",realm=\"%s\",qop=\"%s\",algorithm=\"%s\",uri=\"/Auth\",nonce=\"%s\",nc=%s,cnonce=\"%s\",response=\"%s\"";

  53. sprintf(WWW_Authenticate,format,user,realm,qop,algorithm,nonce,nc,cnonce,rsp);

  54.  
  55. FREE_MALLOC(realm);

  56. FREE_MALLOC(qop);

  57. FREE_MALLOC(algorithm);

  58. FREE_MALLOC(nonce);

  59. FREE_MALLOC(cnonce);

  60. FREE_MALLOC(rsp);

  61. return strdup(WWW_Authenticate);

  62. }

相关阅读

分享到:

栏目导航

推荐阅读

热门阅读