一、DTLS -PSK

PSK 是DTLS 定义的密钥交换方案之一,相对于公钥证书方案(如 ECDHA_RSA) 来说,其具备更加轻量化、高效的优点;

而目前 PSK方案应用也比较广泛。 关于DTLS协议可以看看前面的文章 DTLS要点解析

本次通过模拟的DTLS程序,对DTLS-PSK 握手流程进行抓包分析,以期加深对协议本身的理解。

二、完整握手

流程

         Client                                               Server
------ ------
1.ClientHello --------> <-------- 2..HelloVerifyRequest
(contains cookie) 3.ClientHello -------->
(with cookie)
4.ServerHello
<-------- 5.ServerHelloDone
6.ClientKeyExchange
7.ChangeCipherSpec
8.Finished -------->
9.ChangeCipherSpec
<-------- 10.Finished Application Data <-------> Application Data

步骤解析

~1. Client 发送ClientHello

==[ DTLS Record ]==============================================
Content Type: Handshake (22)
Peer address: localhost/127.0.0.1:5684
Version: 254, 253
Epoch: 0
Sequence Number: 0
Length: 82
Fragment:
Handshake Protocol
Type: CLIENT_HELLO (1)
Peer: localhost/127.0.0.1:5684
Message Sequence No: 0
Fragment Offset: 0
Fragment Length: 70
Length: 70
Version: 254, 253
Random:
GMT Unix Time: Mon Jan 30 22:45:32 CST 2017
Random Bytes: 4B 8B 3C CF 0F 62 57 99 94 E9 86 0A 46 68 BF 44 00 D1 34 45 FC 81 C3 AC BC 55 7E DB
Session ID Length: 0
Cookie Length: 0
Cipher Suites Length: 4
Cipher Suites (2 suites)
Cipher Suite: TLS_PSK_WITH_AES_128_CCM_8
Cipher Suite: TLS_PSK_WITH_AES_128_CBC_SHA256
Compression Methods Length: 1
Compression Methods (1 method)
Compression Method: NULL
Extensions Length: 24
Extension: elliptic_curves (10)
Length: 8
Elliptic Curves Length: 6
Elliptic Curves (3 curves):
Elliptic Curve: secp256r1 (23)
Elliptic Curve: secp384r1 (24)
Elliptic Curve: secp521r1 (25)
Extension: ec_point_formats (11)
Length: 2
EC point formats length: 1
Elliptic Curves Point Formats (1):
EC point format: uncompressed (0)
Extension: server_certificate_type (20)
Server certificate type: RAW_PUBLIC_KEY ===============================================================

此时没有携带Cookie,SessionID未生成;

Cipher Suites 携带了用于协商的算法集。

~2. Server 回复HelloVerifyRequest

	Handshake Protocol
Type: HELLO_VERIFY_REQUEST (3)
Peer: localhost/127.0.0.1:5684
Message Sequence No: 0
Fragment Offset: 0
Fragment Length: 35
Length: 35
Server Version: 254, 253
Cookie Length: 32
Cookie: 77 25 7E 96 9E BD 39 42 94 5F 27 6C 8A 6D 9D D2 1A C9 A3 B8 62 1A 34 86 76 1D D7 AA F4 28 98 6D

Request中携带了32字节的Cookie。

~3. Client 再次发送ClientHello

==[ DTLS Record ]==============================================
Content Type: Handshake (22)
Peer address: localhost/127.0.0.1:5684
Version: 254, 253
Epoch: 0
Sequence Number: 1
Length: 114
Fragment:
Handshake Protocol
Type: CLIENT_HELLO (1)
Peer: localhost/127.0.0.1:5684
Message Sequence No: 1
Fragment Offset: 0
Fragment Length: 102
Length: 102
Version: 254, 253
Random:
GMT Unix Time: Mon Jan 30 22:45:32 CST 2017
Random Bytes: 4B 8B 3C CF 0F 62 57 99 94 E9 86 0A 46 68 BF 44 00 D1 34 45 FC 81 C3 AC BC 55 7E DB
Session ID Length: 0
Cookie Length: 32
Cookie: 77 25 7E 96 9E BD 39 42 94 5F 27 6C 8A 6D 9D D2 1A C9 A3 B8 62 1A 34 86 76 1D D7 AA F4 28 98 6D
Cipher Suites Length: 4
Cipher Suites (2 suites)
Cipher Suite: TLS_PSK_WITH_AES_128_CCM_8
Cipher Suite: TLS_PSK_WITH_AES_128_CBC_SHA256
Compression Methods Length: 1
Compression Methods (1 method)
Compression Method: NULL
Extensions Length: 24
Extension: elliptic_curves (10)
Length: 8
Elliptic Curves Length: 6
Elliptic Curves (3 curves):
Elliptic Curve: secp256r1 (23)
Elliptic Curve: secp384r1 (24)
Elliptic Curve: secp521r1 (25)
Extension: ec_point_formats (11)
Length: 2
EC point formats length: 1
Elliptic Curves Point Formats (1):
EC point format: uncompressed (0)
Extension: server_certificate_type (20)
Server certificate type: RAW_PUBLIC_KEY ===============================================================

本次携带了服务端返回的Cookie值。

~4. Server 回复ServerHello

==[ DTLS Record ]==============================================
Content Type: Handshake (22)
Peer address: /127.0.0.1:64688
Version: 254, 253
Epoch: 0
Sequence Number: 1
Length: 82
Fragment:
Handshake Protocol
Type: SERVER_HELLO (2)
Peer: /127.0.0.1:64688
Message Sequence No: 1
Fragment Offset: 0
Fragment Length: 70
Length: 70
Server Version: 254, 253
Random:
GMT Unix Time: Mon Jan 30 22:45:32 CST 2017
Random Bytes: AB AB 69 55 C4 2E 1F B0 8D B7 FE 7F EA 36 E5 18 6A FD 4D C8 19 4C 73 63 D3 19 B5 E0
Session ID Length: 32
Session ID: 58 8F 51 8C 2A 2A B5 DC 14 9C AB D3 F2 EE BA 25 78 80 47 25 A7 93 35 34 00 D5 CD 53 2C EC B3 D4
Cipher Suite: TLS_PSK_WITH_AES_128_CCM_8
Compression Method: NULL
Extensions Length: 0
===============================================================

此时生成了32字节的 SessionID;

CipherSute 指明了即将采用的密钥算法集;

~5. Server 回复ServerHelloDone

==[ DTLS Record ]==============================================
Content Type: Handshake (22)
Peer address: /127.0.0.1:64688
Version: 254, 253
Epoch: 0
Sequence Number: 2
Length: 12
Fragment:
Handshake Protocol
Type: SERVER_HELLO_DONE (14)
Peer: /127.0.0.1:64688
Message Sequence No: 2
Fragment Offset: 0
Fragment Length: 0
Length: 0 ===============================================================

Server 通知ServerHello结束。

~6. Client 发送ClientKeyExchange

==[ DTLS Record ]==============================================
Content Type: Handshake (22)
Peer address: localhost/127.0.0.1:5684
Version: 254, 253
Epoch: 0
Sequence Number: 2
Length: 23
Fragment:
Handshake Protocol
Type: CLIENT_KEY_EXCHANGE (16)
Peer: localhost/127.0.0.1:5684
Message Sequence No: 2
Fragment Offset: 0
Fragment Length: 11
Length: 11
PSK Identity: 012345678 ===============================================================

PSK Identity 用于标识终端ID,后续双方根据该ID找到终端预置的PSK用于计算密钥;

~7. Client 发送ChangeCipherSpec

==[ DTLS Record ]==============================================
Content Type: Change Cipher Spec (20)
Peer address: localhost/127.0.0.1:5684
Version: 254, 253
Epoch: 0
Sequence Number: 3
Length: 1
Fragment:
Change Cipher Spec Message ===============================================================

Client 表示已经确认算法和密钥。

~8. Client 发送Finish

==[ DTLS Record ]==============================================
Content Type: Handshake (22)
Peer address: localhost/127.0.0.1:5684
Version: 254, 253
Epoch: 1
Sequence Number: 0
Length: 40
Fragment:
Handshake Protocol
Type: FINISHED (20)
Peer: localhost/127.0.0.1:5684
Message Sequence No: 3
Fragment Offset: 0
Fragment Length: 12
Length: 12
Verify Data: BC 00 D7 F6 6F E5 A4 B4 0D 8B 5C 8A ===============================================================

Client 表示握手完成,其中Verify Data是由动态密钥计算得出的摘要,用于Server端验证。

~9. Server 发送ChangeCipherSpec

==[ DTLS Record ]==============================================
Content Type: Change Cipher Spec (20)
Peer address: /127.0.0.1:64688
Version: 254, 253
Epoch: 0
Sequence Number: 3
Length: 1
Fragment:
Change Cipher Spec Message ===============================================================

Server 表示已经确认算法和密钥。

~10. Server 发送Finish

==[ DTLS Record ]==============================================
Content Type: Handshake (22)
Peer address: /127.0.0.1:64688
Version: 254, 253
Epoch: 1
Sequence Number: 0
Length: 40
Fragment:
Handshake Protocol
Type: FINISHED (20)
Peer: /127.0.0.1:64688
Message Sequence No: 3
Fragment Offset: 0
Fragment Length: 12
Length: 12
Verify Data: EC C9 5E 4E 24 BE 77 78 CB F5 20 54 ===============================================================

Server 表示握手完成,其中Verify Data是由动态密钥计算得出的摘要,用于Client端验证。

至此,完整的PSK 握手流程已经结束,接下来执行会话恢复的场景。

三、会话恢复

流程

         Client                                               Server
------ ------
1.ClientHello --------> <-------- 2..HelloVerifyRequest
(contains cookie) 3.ClientHello -------->
(with cookie)
4.ServerHello
5.ChangeCipherSpec
<-------- 6.Finished 7.ChangeCipherSpec
8.Finished --------> Application Data <-------> Application Data

步骤解析

~1. Client 发送ClientHello

==[ DTLS Record ]==============================================
Content Type: Handshake (22)
Peer address: /127.0.0.1:5684
Version: 254, 253
Epoch: 0
Sequence Number: 0
Length: 115
Fragment:
Handshake Protocol
Type: CLIENT_HELLO (1)
Peer: /127.0.0.1:5684
Message Sequence No: 0
Fragment Offset: 0
Fragment Length: 103
Length: 103
Version: 254, 253
Random:
GMT Unix Time: Tue Jan 31 00:02:05 CST 2017
Random Bytes: B6 28 F7 76 FE C1 B9 7A 87 CE D9 81 2D C3 9A AA 07 F8 69 2D 36 A3 B3 A2 1F 47 E1 FF
Session ID Length: 32
Session ID: 58 8F 63 71 DE B4 87 9A C0 0B 67 BB 16 7F 33 1C B6 FF E2 74 74 D9 EB 58 D4 78 44 BA 4C 22 42 38
Cookie Length: 0
Cipher Suites Length: 4
Cipher Suites (2 suites)
Cipher Suite: TLS_NULL_WITH_NULL_NULL
Cipher Suite: TLS_PSK_WITH_AES_128_CCM_8
Compression Methods Length: 2
Compression Methods (2 method)
Compression Method: NULL
Compression Method: NULL
Extensions Length: 24
Extension: elliptic_curves (10)
Length: 8
Elliptic Curves Length: 6
Elliptic Curves (3 curves):
Elliptic Curve: secp256r1 (23)
Elliptic Curve: secp384r1 (24)
Elliptic Curve: secp521r1 (25)
Extension: ec_point_formats (11)
Length: 2
EC point formats length: 1
Elliptic Curves Point Formats (1):
EC point format: uncompressed (0)
Extension: server_certificate_type (20)
Server certificate type: RAW_PUBLIC_KEY ===============================================================

此时SessionID已经存在,但Cookie为空,仍然需要经过一次HelloVerify。

~2.Server 发送HelloVerifyRequest

	Handshake Protocol
Type: HELLO_VERIFY_REQUEST (3)
Peer: /127.0.0.1:5684
Message Sequence No: 0
Fragment Offset: 0
Fragment Length: 35
Length: 35
Server Version: 254, 253
Cookie Length: 32
Cookie: 67 6B 86 62 06 F6 A4 3D 31 59 B1 82 80 39 23 76 C6 2C E2 FC E1 7F 41 E8 EE 13 6C 12 A6 76 7B C5

Server 发送了32字节的Cookie值

~3. Client 再次发送ClientHello

==[ DTLS Record ]==============================================
Content Type: Handshake (22)
Peer address: /127.0.0.1:5684
Version: 254, 253
Epoch: 0
Sequence Number: 1
Length: 147
Fragment:
Handshake Protocol
Type: CLIENT_HELLO (1)
Peer: /127.0.0.1:5684
Message Sequence No: 1
Fragment Offset: 0
Fragment Length: 135
Length: 135
Version: 254, 253
Random:
GMT Unix Time: Tue Jan 31 00:02:05 CST 2017
Random Bytes: B6 28 F7 76 FE C1 B9 7A 87 CE D9 81 2D C3 9A AA 07 F8 69 2D 36 A3 B3 A2 1F 47 E1 FF
Session ID Length: 32
Session ID: 58 8F 63 71 DE B4 87 9A C0 0B 67 BB 16 7F 33 1C B6 FF E2 74 74 D9 EB 58 D4 78 44 BA 4C 22 42 38
Cookie Length: 32
Cookie: 67 6B 86 62 06 F6 A4 3D 31 59 B1 82 80 39 23 76 C6 2C E2 FC E1 7F 41 E8 EE 13 6C 12 A6 76 7B C5
Cipher Suites Length: 4
Cipher Suites (2 suites)
Cipher Suite: TLS_NULL_WITH_NULL_NULL
Cipher Suite: TLS_PSK_WITH_AES_128_CCM_8
Compression Methods Length: 2
Compression Methods (2 method)
Compression Method: NULL
Compression Method: NULL
Extensions Length: 24
Extension: elliptic_curves (10)
Length: 8
Elliptic Curves Length: 6
Elliptic Curves (3 curves):
Elliptic Curve: secp256r1 (23)
Elliptic Curve: secp384r1 (24)
Elliptic Curve: secp521r1 (25)
Extension: ec_point_formats (11)
Length: 2
EC point formats length: 1
Elliptic Curves Point Formats (1):
EC point format: uncompressed (0)
Extension: server_certificate_type (20)
Server certificate type: RAW_PUBLIC_KEY ===============================================================

此时Cookie和SessionID都不为空。

~4. Server 发送ServerHello

==[ DTLS Record ]==============================================
Content Type: Handshake (22)
Peer address: /127.0.0.1:54595
Version: 254, 253
Epoch: 0
Sequence Number: 1
Length: 82
Fragment:
Handshake Protocol
Type: SERVER_HELLO (2)
Peer: /127.0.0.1:54595
Message Sequence No: 1
Fragment Offset: 0
Fragment Length: 70
Length: 70
Server Version: 254, 253
Random:
GMT Unix Time: Tue Jan 31 00:02:05 CST 2017
Random Bytes: 6B 21 0D B0 A3 33 A3 49 65 0E D9 D1 DB 0E 62 74 51 EE 1B E1 CC 37 1E FD 8C 67 39 00
Session ID Length: 32
Session ID: 58 8F 63 71 DE B4 87 9A C0 0B 67 BB 16 7F 33 1C B6 FF E2 74 74 D9 EB 58 D4 78 44 BA 4C 22 42 38
Cipher Suite: TLS_PSK_WITH_AES_128_CCM_8
Compression Method: NULL
===============================================================

~5. Server 发送ChangeCipherSpec

==[ DTLS Record ]==============================================
Content Type: Change Cipher Spec (20)
Peer address: /127.0.0.1:54595
Version: 254, 253
Epoch: 0
Sequence Number: 2
Length: 1
Fragment:
Change Cipher Spec Message ===============================================================

Server 表示已经确认算法和密钥。

~6. Server 发送Finish

==[ DTLS Record ]==============================================
Content Type: Handshake (22)
Peer address: /127.0.0.1:54595
Version: 254, 253
Epoch: 1
Sequence Number: 0
Length: 40
Fragment:
Handshake Protocol
Type: FINISHED (20)
Peer: /127.0.0.1:54595
Message Sequence No: 2
Fragment Offset: 0
Fragment Length: 12
Length: 12
Verify Data: D7 8F CA EC 97 B7 96 A3 CD 5E 5C 97 ===============================================================

Server 表示握手完成,其中VerifyData 用于Client端验证。

~7. Client 发送ChangeCipherSpec

==[ DTLS Record ]==============================================
Content Type: Change Cipher Spec (20)
Peer address: /127.0.0.1:5684
Version: 254, 253
Epoch: 0
Sequence Number: 2
Length: 1
Fragment:
Change Cipher Spec Message ===============================================================

Client 表示已经确认算法和密钥。

~8. Client 发送Finish

==[ DTLS Record ]==============================================
Content Type: Handshake (22)
Peer address: /127.0.0.1:5684
Version: 254, 253
Epoch: 1
Sequence Number: 0
Length: 40
Fragment:
Handshake Protocol
Type: FINISHED (20)
Peer: /127.0.0.1:5684
Message Sequence No: 2
Fragment Offset: 0
Fragment Length: 12
Length: 12
Verify Data: 3F 86 FC 45 D8 41 A6 BE BD 54 C2 7A ===============================================================

Client 表示握手完成,其中VerifyData 用于Server端验证。

至此,会话恢复(Resuming)流程分析完毕。

四、相关参考

Scadium 是一个纯Java语言的DTLS实现,可支持DTLS 1.2版本。

目前其被纳入 Californium项目(Coap技术框架),项目地址为:

https://github.com/eclipse/californium/tree/master/scandium-core

关于DTLS的技术纪要:

http://www.cnblogs.com/littleatp/p/6358261.html

TLS_PSK 标准定义:

https://tools.ietf.org/html/rfc4279

DTLS-PSK算法抓包解析的更多相关文章

  1. 使用Charles进行网络请求抓包解析

    使用Charles进行网络请求抓包解析 0. 懒人的福音(⌐■_■)(破解版下载地址,记得安装java库支持) http://pan.baidu.com/s/1c08ksMW 1. 查看电脑的ip地址 ...

  2. BLE广播数据的抓包解析

    前言: 报文由数据字节组成同时是按比特传输的,这就免不了牵涉到字节序的问题. 对于各个字节的传输,总是从最低位开始传输.如0x80是按00000001发送的,0x01是按10000000发送的. 同时 ...

  3. Android抓包解析全过程

    需求原因 在android开发中,遇到socket编程,无法从log日志中查看到与之通讯的socket发送和返回的数据包是什么,这里介绍一个工具,tcpdump工具和wireshark工具查看抓到的内 ...

  4. 【Android测试工具】Android抓包解析全过程

    需求原因 在android开发中,遇到socket编程,无法从log日志中查看到与之通讯的socket发送和返回的数据包是什么,这里介绍一个工具,tcpdump工具和wireshark工具查看抓到的内 ...

  5. Asp.Net_抓包解析xml文件为json

    protected void Button1_Click(object sender, EventArgs e) { string Phone = this.Txt_Con.Text; string ...

  6. Fiddler抓包工具使用详解

    一.Fiddler简介 Fiddler是最强大最好用的Web调试工具之一, 它能记录所有客户端和服务器的http和https请求.允许你监视.设置断点.甚至修改输入输出数据.Fiddler包含了一个强 ...

  7. fiddler抓包时显示Tunnel to......443是怎么回事

    之前公司的app使用的http协议,因此不需要安装证书也能够转包. 后来改成https协议后,在使用fiddler进行抓包时,一直出现tunnel to 443. 百度了好久也没有具体的解决办法,后来 ...

  8. Fiddler抓包工具使用方法

    Fiddler是最强大最好用的Web调试工具之一, 它能记录所有客户端和服务器的http和https请求.允许你监视.设置断点.甚至修改输入输出数据.Fiddler包含了一个强大的基于事件脚本的子系统 ...

  9. 玩转Fiddler抓包工具

    一.Fiddler简述 Fiddler是最强大最好用的Web调试工具之一, 它能记录所有客户端和服务器的http和https请求.允许你监视.设置断点.甚至修改输入输出数据.Fiddler包含了一个强 ...

随机推荐

  1. JQuery的方便之处——宽高设置、坐标值和滚动条+事件绑定机制

    1.元素的宽高 可以通过css来进行设置,例如:$("元素").css({"宽度":"值","高度":"值&q ...

  2. 在XAMPP上建立多个域名的站点

    XAMPP默认安装完毕后,站点文件默认放在/xampp/htdocs/ 文件下,并且可以通过http://localhost 进行访问.先前在测试各种程序的时候均是在/xampp/htdocs/ 文件 ...

  3. [Big Data]Spark

    SPARK Spark是UC Berkeley AMP lab (加州大学伯克利分校的AMP实验室)所开源的类Hadoop MapReduce的通用并行框架,Spark,拥有Hadoop MapRed ...

  4. iOS开发——缩放图片

    捏合图片,在原imageView中放大缩小图片. 懒得多说,直接上代码: // //  ViewController.m //  Demo-hehehe // //  Created by yyt o ...

  5. MPU6050程序(转)

    源:MPU6050程序 初始化定义 #ifndef _MPU6050_H #define _MPU6050_H #define PORT_USED 0 #define MPU6050_ADDRESS_ ...

  6. List学习笔记

    List 特点:1.有序.2.可重复. ArrayList: 底层是数组,数组是有下标的. 会自动扩容,底层默认初始化容量是10,扩大之后的容量预设是原来容量的一半(jdk 1.8).以前好像是原容量 ...

  7. xml数据传输

  8. C# .NET中的 反射的应用

    C#中的映射 C#编译后的文件主要由IL代码和元数据组成,元数据为.NET组件提供了丰富的自描述特性,它使得我们可以在代码运行时获知组件中的类型等重要的信息.C#中这是通过一种称作映射(Reflect ...

  9. python 数组的del ,remove,pop区别

    以a=[1,2,3] 为例,似乎使用del, remove, pop一个元素2 之后 a都是为 [1,3], 如下: >>> a=[1,2,3] >>> a.rem ...

  10. oracle系列--解锁数据库

    一.安装完成后解锁Scott数据库步骤: 进入SQL Plus 请输入用户名:sys输入口令:sys as sysdba  //这里的口令是不可见的,注意空格SQL> alter user sc ...