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

相关文章

Nginx分布式部署流程分析

《Nginx分布式部署流程分析》文章介绍Nginx在分布式部署中的反向代理和负载均衡作用,用于分发请求、减轻服务器压力及解决session共享问题,涵盖配置方法、策略及Java项目应用,并提及分布式事... 目录分布式部署NginxJava中的代理代理分为正向代理和反向代理正向代理反向代理Nginx应用场景

Python函数作用域与闭包举例深度解析

《Python函数作用域与闭包举例深度解析》Python函数的作用域规则和闭包是编程中的关键概念,它们决定了变量的访问和生命周期,:本文主要介绍Python函数作用域与闭包的相关资料,文中通过代码... 目录1. 基础作用域访问示例1:访问全局变量示例2:访问外层函数变量2. 闭包基础示例3:简单闭包示例4

Redis中Hash从使用过程到原理说明

《Redis中Hash从使用过程到原理说明》RedisHash结构用于存储字段-值对,适合对象数据,支持HSET、HGET等命令,采用ziplist或hashtable编码,通过渐进式rehash优化... 目录一、开篇:Hash就像超市的货架二、Hash的基本使用1. 常用命令示例2. Java操作示例三

Redis中Set结构使用过程与原理说明

《Redis中Set结构使用过程与原理说明》本文解析了RedisSet数据结构,涵盖其基本操作(如添加、查找)、集合运算(交并差)、底层实现(intset与hashtable自动切换机制)、典型应用场... 目录开篇:从购物车到Redis Set一、Redis Set的基本操作1.1 编程常用命令1.2 集

Linux下利用select实现串口数据读取过程

《Linux下利用select实现串口数据读取过程》文章介绍Linux中使用select、poll或epoll实现串口数据读取,通过I/O多路复用机制在数据到达时触发读取,避免持续轮询,示例代码展示设... 目录示例代码(使用select实现)代码解释总结在 linux 系统里,我们可以借助 select、

Redis中的有序集合zset从使用到原理分析

《Redis中的有序集合zset从使用到原理分析》Redis有序集合(zset)是字符串与分值的有序映射,通过跳跃表和哈希表结合实现高效有序性管理,适用于排行榜、延迟队列等场景,其时间复杂度低,内存占... 目录开篇:排行榜背后的秘密一、zset的基本使用1.1 常用命令1.2 Java客户端示例二、zse

Redis中的AOF原理及分析

《Redis中的AOF原理及分析》Redis的AOF通过记录所有写操作命令实现持久化,支持always/everysec/no三种同步策略,重写机制优化文件体积,与RDB结合可平衡数据安全与恢复效率... 目录开篇:从日记本到AOF一、AOF的基本执行流程1. 命令执行与记录2. AOF重写机制二、AOF的

k8s中实现mysql主备过程详解

《k8s中实现mysql主备过程详解》文章讲解了在K8s中使用StatefulSet部署MySQL主备架构,包含NFS安装、storageClass配置、MySQL部署及同步检查步骤,确保主备数据一致... 目录一、k8s中实现mysql主备1.1 环境信息1.2 部署nfs-provisioner1.2.

springboot依靠security实现digest认证的实践

《springboot依靠security实现digest认证的实践》HTTP摘要认证通过加密参数(如nonce、response)验证身份,避免明文传输,但存在密码存储风险,相比基本认证更安全,却因... 目录概述参数Demopom.XML依赖Digest1Application.JavaMyPasswo

Python中isinstance()函数原理解释及详细用法示例

《Python中isinstance()函数原理解释及详细用法示例》isinstance()是Python内置的一个非常有用的函数,用于检查一个对象是否属于指定的类型或类型元组中的某一个类型,它是Py... 目录python中isinstance()函数原理解释及详细用法指南一、isinstance()函数