http digest认证过程分析及例子(这个给出了提取函数)

2023-05-16

转载自:http digest认证过程分析及例子_希哈科技的博客-CSDN博客

http digest认证过程分析及例子

技术标签: http  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. assert(realm && nonce && algorithm && qop);

  32. //FIXME

  33. char *nc = "00000001";

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

  35. char A1[100] = {0};

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

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

  38. char A2[80] = {0};

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

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

  41. char contact[512] = {0};

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

  43. FREE_MALLOC(md5_A1);

  44. FREE_MALLOC(md5_A2);

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

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

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

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

  49. FREE_MALLOC(realm);

  50. FREE_MALLOC(qop);

  51. FREE_MALLOC(algorithm);

  52. FREE_MALLOC(nonce);

  53. FREE_MALLOC(cnonce);

  54. FREE_MALLOC(rsp);

  55. return strdup(WWW_Authenticate);

  56. }

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:http digest认证过程分析及例子_希哈科技的博客-CSDN博客

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

http digest认证过程分析及例子(这个给出了提取函数) 的相关文章

随机推荐

  • 记一次Ubuntu 18.04 内核升级(转载)

    https zhuanlan zhihu com p 75669680 记一次Ubuntu 18 04 内核升级 CCCCCaO 闷骚得很 前言 原本使用 Ubuntu Server 18 04 2 LTS 来搭建一个NextCloud服务
  • 有图形化工具升级linux内核 ukuu-gtk

    有图形化工具升级linux内核 ukuu gtk https zhuanlan zhihu com p 75669680 转载自 xff1a https os 51cto com art 201905 596204 htm 借助UKUU在U
  • 网络通信1—TCP程序编写步骤(参照 Ubuntu 16.04 版本)(转载)

    转载自 xff1a https blog csdn net wenfei11471 article details 79478105 网络通信1 TCP程序编写步骤 xff08 参照 Ubuntu 16 04 版本 xff09 wenfei
  • select实现简单TCP通信(ubuntu 18.04)

    用法 cilent IP地址 然后在终端输入内容敲回车就可以了 端口号在代码里写的 转载自 xff1a https www cnblogs com soldierback p 10701684 html select实现简单TCP通信 ub
  • C语言网络编程(2)— TCP通信(转载)

    转载自 xff1a C语言网络编程 xff08 2 xff09 TCP通信 Willliam william的博客 CSDN博客 c语言tcp网络编程 C语言网络编程 xff08 2 xff09 TCP通信 Willliam william
  • 在ubuntu+vs code上通过HTTP协议实现服务器端和客户端通信(转载)

    转载自 xff1a 在ubuntu 43 vs code上通过HTTP协议实现服务器端和客户端通信 DP323的博客 CSDN博客 在 ubuntu 43 vs code上通过HTTP协议实现服务器端和客户端通信 DP323 2021 06
  • 常见元件、封装及尺寸

    封装类型 贴片元器件封装形式是半导体器件的一种封装形式 表面贴片技术 xff08 SMT xff09 所涉及的零件种类繁多 xff0c 有许多已形成业界通用标准 xff0c 这主要是一些芯片电容电阻等 xff1b 有许多仍在不断的变化 xf
  • C++:C语言实现HTTP的GET和POST请求

    似乎写代码发HTTP请求需要自己把完整的协议帧写出来 xff1f 而不是单纯填个URL就行了 xff0c 那既然完整协议帧都写出来了 xff0c 那我直接TCP发不就可以了 xff1f 还是说不能这样 基本你百度搜 c 43 43 发送ht
  • 字符串连接函数strcat,这个可以好好用用

    字符串连接函数strcat xff0c 这个可以好好用用 C 43 43 xff1a C语言实现HTTP的GET和POST请求 方方和圆圆 博客园 C 43 43 编写的Linux下Http请求 c 43 43 httplinux 其它代码
  • HTTP 账号密码验证

    http请求带用户名和密码验证 wdk的博客 CSDN博客 url带用户名和密码 转载自 xff1a HTTP 账号密码验证 qq 25600055的专栏 CSDN博客 HTTP 账号 密码验证 173300598 2018 05 28 1
  • http authorization 基本认证

    转载自 xff1a http authorization 基本认证 szchenrong 博客园 http authorization 基本认证 http协议是无状态的 xff0c 浏览器和web服务器之间可以通过cookie来身份识别 桌
  • HTTP报文(这篇讲得比较详细)

    转载自 xff1a HTTP报文 逆心 博客园 之前写过一篇HTML报文 xff0c 但是感觉写完之后还是不懂 xff0c 最近终于有时间开始看 HTTP权威指南 xff0c 看完之后觉得还是比之前的理解更加深入了 xff0c 提取HTTP
  • libcurl可以发送http请求

    libcurl可以发送http请求 轻量级C 43 43 实现的httpserver和httpclient c 43 43 httpserver C 43 43 代码类资源 CSDN下载 转 libcurl库使用方法 xff0c 好长 xf
  • Http Get请求简单实例(C++)

    转载自 xff1a 掘金 https juejin cn post 6844903897161531400 Http Get请求简单实例 xff08 C 43 43 xff09 HTTP报文 HTTP请求报文 请求行 xff1a GET H
  • 纯C++实现的HTTP请求封装(POST/GET)

    转载自 xff1a 纯C 43 43 实现的HTTP请求封装 xff08 POST GET xff09 阳光柠檬 的技术笔记 CSDN博客 纯C 43 43 实现的HTTP请求封装 xff08 POST GET xff09 时间 2016
  • [转]www-authenticate认证过程浅析

    转载自 xff1a 转 www authenticate认证过程浅析 maoliran的博客 CSDN博客 转 www authenticate认证过程浅析 新一下的兰天 2016 07 06 18 22 20 18002 正在上传 重新上
  • http Digest认证计算方法整理

    摘要认证及实现HTTP digest authentication 简书 HTTP Basic和Digest认证介绍与计算 诸子流 博客园 不要不知道上面说的URI是什么意思啊 图解HTTP 第 8 章 确认访问用户身份的认证 简书 8 1
  • HTTP Basic和Digest认证介绍与计算

    转载自 xff1a HTTP Basic和Digest认证介绍与计算 诸子流 博客园 HTTP Basic和Digest认证介绍与计算 一 说明 web用户认证 xff0c 最开始是get提交 43 把用户名密码存放在客户端的cookie中
  • 一些十分有用的字符串相关的函数,在写串口解析,TCP HTTP通信时有用

    写串口TCP HTTP xff0c 这方面感受颇深 xff0c 也遇到不少对吧 写数据解析时的常用函数 在字符串中查找字符串 strstr 我觉得有了str很多解析写起来会轻松快很多 字符串合并函数 字符串连接函数strcat xff0c
  • http digest认证过程分析及例子(这个给出了提取函数)

    转载自 xff1a http digest认证过程分析及例子 希哈科技的博客 CSDN博客 http digest认证过程分析及例子 技术标签 xff1a http digest 认证 验证过程 xff1a 步骤一 客户端向服务器申请数据