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

2023-12-18 03:38

本文主要是介绍http digest认证过程分析及例子(这个给出了提取函数),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

转载自: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博客

 

这篇关于http digest认证过程分析及例子(这个给出了提取函数)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/506923

相关文章

postgresql使用UUID函数的方法

《postgresql使用UUID函数的方法》本文给大家介绍postgresql使用UUID函数的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录PostgreSQL有两种生成uuid的方法。可以先通过sql查看是否已安装扩展函数,和可以安装的扩展函数

MySQL字符串常用函数详解

《MySQL字符串常用函数详解》本文给大家介绍MySQL字符串常用函数,本文结合实例代码给大家介绍的非常详细,对大家学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录mysql字符串常用函数一、获取二、大小写转换三、拼接四、截取五、比较、反转、替换六、去空白、填充MySQL字符串常用函数一、

SpringBoot中六种批量更新Mysql的方式效率对比分析

《SpringBoot中六种批量更新Mysql的方式效率对比分析》文章比较了MySQL大数据量批量更新的多种方法,指出REPLACEINTO和ONDUPLICATEKEY效率最高但存在数据风险,MyB... 目录效率比较测试结构数据库初始化测试数据批量修改方案第一种 for第二种 case when第三种

解决1093 - You can‘t specify target table报错问题及原因分析

《解决1093-Youcan‘tspecifytargettable报错问题及原因分析》MySQL1093错误因UPDATE/DELETE语句的FROM子句直接引用目标表或嵌套子查询导致,... 目录报js错原因分析具体原因解决办法方法一:使用临时表方法二:使用JOIN方法三:使用EXISTS示例总结报错原

C++中assign函数的使用

《C++中assign函数的使用》在C++标准模板库中,std::list等容器都提供了assign成员函数,它比操作符更灵活,支持多种初始化方式,下面就来介绍一下assign的用法,具有一定的参考价... 目录​1.assign的基本功能​​语法​2. 具体用法示例​​​(1) 填充n个相同值​​(2)

MySql基本查询之表的增删查改+聚合函数案例详解

《MySql基本查询之表的增删查改+聚合函数案例详解》本文详解SQL的CURD操作INSERT用于数据插入(单行/多行及冲突处理),SELECT实现数据检索(列选择、条件过滤、排序分页),UPDATE... 目录一、Create1.1 单行数据 + 全列插入1.2 多行数据 + 指定列插入1.3 插入否则更

Linux进程CPU绑定优化与实践过程

《Linux进程CPU绑定优化与实践过程》Linux支持进程绑定至特定CPU核心,通过sched_setaffinity系统调用和taskset工具实现,优化缓存效率与上下文切换,提升多核计算性能,适... 目录1. 多核处理器及并行计算概念1.1 多核处理器架构概述1.2 并行计算的含义及重要性1.3 并

PostgreSQL中rank()窗口函数实用指南与示例

《PostgreSQL中rank()窗口函数实用指南与示例》在数据分析和数据库管理中,经常需要对数据进行排名操作,PostgreSQL提供了强大的窗口函数rank(),可以方便地对结果集中的行进行排名... 目录一、rank()函数简介二、基础示例:部门内员工薪资排名示例数据排名查询三、高级应用示例1. 每

Spring boot整合dubbo+zookeeper的详细过程

《Springboot整合dubbo+zookeeper的详细过程》本文讲解SpringBoot整合Dubbo与Zookeeper实现API、Provider、Consumer模式,包含依赖配置、... 目录Spring boot整合dubbo+zookeeper1.创建父工程2.父工程引入依赖3.创建ap

Linux下进程的CPU配置与线程绑定过程

《Linux下进程的CPU配置与线程绑定过程》本文介绍Linux系统中基于进程和线程的CPU配置方法,通过taskset命令和pthread库调整亲和力,将进程/线程绑定到特定CPU核心以优化资源分配... 目录1 基于进程的CPU配置1.1 对CPU亲和力的配置1.2 绑定进程到指定CPU核上运行2 基于