第一部分:HCI协议(八)具体的HCI command和event(16、AUTHENTICATION AND ENCRYPTION 二)
(1)Link Key Notification Event
当controller发送Link Key Notification event给host时,表示本地设备与地址为BD_ADDR的远端设备之间的连接已经产生了一个新的Link key,此时host可以将这个Link key存起来,也可以使用Write_Stored_Link_Key将link key存到controller内。
参数说明:
BD_ADDR(6 Octet):远端设备地址
Link_Key(16 Octet):与BD_ADDR相关的link key
Key_Type(1 Octet):取值范围如下:
Combination Key:使用标准配对模式(PIN Code)时
Local Unit Key:
Remote Unit Key:
Debug Combination Key:使用Simple Pairing模式时,并且接收或者发送debug public key
Unauthenticated Combination Key generated from P-192:使用Simple Pairing模式和Just Works association model时
Authenticated Combination Key generated from P-192:使用Simple Pairing模式,但是不使用Just Works association model时
Changed Combination Key:使用Change Connection Link Key程序改变Link key,并且Simple Pairing模式是enable时
Unauthenticated Combination Key generated from P-256:使用Simple Pairing模式,但是不使用Just Works association model时(使用Write Secure Connections Host Support Command设置Secure_Connections_Host_Support为enable)
Authenticated Combination Key generated from P-256:使用Change Connection Link Key程序改变Link key,并且Simple Pairing模式是enable时(使用Write Secure Connections Host Support Command设置Secure_Connections_Host_Support为enable)
Secure Simple Pairing有四种关联模型,分别是Numeric Comparison、Just Works、Out Of Band、Passkey Entry,这四种模式具体使用哪一种在I/O capabilities 交互的时候确定的
Numeric Comparison:这种模型适用于两个设备都能显示6个十进制数字并且都可以输入yes或者no的场景,例如:PC和手机
Just Works:这种模式适用于两个设备中至少一个设备不支持显示也不支持输入6个十进制数字,例如:手机和耳机
Out Of Band:这种模型是使用一种带外机制发现设备以及配对过程中的交互或者传输密码,例如:NFC
Passkey Entry:这种模型适用于一个设备有输入能力但是没有显示6个十进制数字的能力,另一个设备有显示(输出)能力的场景,例如:PC和键盘,PC显示6个数字,然后在键盘输入这6个数字
combination key和Unit key在功能上是没有区别的,区别在于产生的方式,unit key是由单一设备产生的,一般是固定不变的,combination key是根据两个设备的信息交互产生的,是可变的。一般设备存储key的内存很小或者需要将key固定在设备里的时候会使用unit key,当前unit key的安全性没有combination key高
举例说明:
0x04:表示HCI Event Packet
0x18:表示这个event是Link Key Notification event
0x17:表示Parameter_Total_Length即23个byte
0xeb 0x01 0xce 0x55 0x36 0x34:表示远端设备地址是0x34 0x36 0x55 0xce 0x01 0xeb
Link Key: 0xa4 0x31 0x67 0x79 0x20 0xb4 0x71 0x97 0x05 0x8d 0x7a 0x3b 0x82 0x11 0x84 0x8a
0x04:表示key_type是Unauthenticated Combination Key generated from P-192
(2)Authentication Requested Command
这个命令用来请求对远端设备进行认证,Connection_Handle表示本地设备和远端设备之间的ACL连接
注意:如果认证失败,不会自动断开连接,需要host发起断开动作
这个命令的后续动作:
第一步:host发送Authentication_Requested以后,会立即收到一个Command Status event表示Authentication_Requested是否成功。
第二步:
A)如果Simple Pairing Mode是enabled,host会收到一个Link Key Request event,此时
a)Host如果回复Link Key Request Negative Reply Command,认证失败,那么就会开始Simple Pairing
b)Host如果回复Link Key Request Reply Command,则继续认证过程
B)如果Simple Pairing Mode不是enabled,host会收到一个Link Key Request event,此时
a)Host如果回复Link Key Request Negative Reply Command,认证失败,那么就会开始PIN code认证,host会收到PIN Code Request event
b)Host如果回复Link Key Request Reply Command,则继续认证过程
第三步:认证成功,host会接收到Authentication Complete event
参数说明:
Connection_Handle(2 Octet):表示一个ACL连接(注意:这个只是用来确定两个BR/EDR Controller,认证的是两个BR/EDR Controller之间所有的连接,不是只有Connection_Handle表示的连接)
举例说明:
Command如下:
0x01:表示HCI Command Packet
0x0411:表示是HCI_Authentication_Requested command
指令的OGF是0x01即0000 01
OCF是0x0011即00 0001 0001
所以整个opcode是0000 0100 0001 0001即0x0411
0x2:表示Parameter_Total_Length=0x2,即参数总长度是2个byte
0x000d:表示Connection_Handle
对应的Event:
Command Status event:
0x04:表示HCI Event Packet
0x0f:表示这个event是command status event
0x04:表示Parameter_Total_Length即4个byte
0x00:表示status为0
0x01:表示Num_HCI_Command_Packets
0x0411:表示是HCI_Authentication_Requested command
之后
host收到Link Key Request event->
host没有link key,所以回复Link Key Request Negative Reply Command->
认证失败->
host收到PIN Code Request event->
PIN_Code_Request_Reply->
PIN code配对完成收到Link Key Notification event->
认证完成,收到Authentication Complete event
如下图所示:
(3)Authentication Complete Event
当认证完成时,host会收到这个event
参数说明:
Status(1 Octet):0x00表示成功,其他值表示失败
Connection_Handle(2 Octet):表示一个ACL连接
举例说明:
0x04:表示HCI Event Packet
0x06:表示这个event是Authentication Complete event
0x03:表示Parameter_Total_Length即3个byte
0x00:表示status为0
0x000d:表示Connection_Handle
(4)Set Connection Encryption Command
这个命令可以enable/disable连接层的加密
参数说明:
Connection_Handle(2 Octet):表示一个ACL连接,用来确定与本地设备连接的远端BR/EDR Controller
Encryption_Enable(1 Octet):0x00表示Turn Link Level Encryption OFF(关闭连接层加密),0x01表示Turn Link Level Encryption ON(开启连接层加密)
注意:当加密改变时,所以的与远端设备所有的ACL通信都会关闭;
当本地和远端设备都支持Secure Connections (Controller Support)和Secure Connections (Host Support) features时,如果Connection_Handle的加密是enable,此时设置Encryption_Enable为Turn Link Level Encryption OFF,controller会报错:Encryption Mode Not Acceptable (0x25).
举例说明:
Command如下:
0x01:表示HCI Command Packet
0x0413:表示是HCI_Set_Connection_Encryption command
指令的OGF是0x01即0000 01
OCF是0x0013即00 0001 0011
所以整个opcode是0000 0100 0001 0011即0x0413
0x3:表示Parameter_Total_Length=0x3,即参数总长度是3个byte
0x0003:表示Connection_Handle
0x01:表示开启连接层加密
对应的Event:
Command Status event:
0x04:表示HCI Event Packet
0x0f:表示这个event是command status event
0x04:表示Parameter_Total_Length即4个byte
0x00:表示status为0
0x01:表示Num_HCI_Command_Packets
0x0413:表示是HCI_Set_Connection_Encryption command
当加密完成时,会收到Encryption Change Event
Encryption Change Event
0x04:表示HCI Event Packet
0x08:表示这个event是Encryption Change event
0x04:表示Parameter_Total_Length即4个byte
0x00:表示status为0
0x0003:表示Connection_Handle
0x01:表示使用E0方式进行连接层加密(BR/EDR)
(5)Encryption Change Event
这个event表示连接层加密模式已经修改完成
参数说明:
Status(1 Octet):0x00表示成功,其他值表示失败
Connection_Handle(2 Octet):表示一个ACL连接
Encryption_Enabled(1 Octet):表示修改后的加密模式,这个参数值得含义跟Secure_Connections_Host_Support(通过Write Secure Connections Host Support Command进行配置)和Connection_Handle有关:
Secure_Connections_Host_Support是disable时,Encryption_Enabled可以是0x00或者0x01
Connection_Handle表示的是LE连接时,Encryption_Enabled可以是0x00或者0x01
Secure_Connections_Host_Support是enable并且Connection_Handle是一个BR/EDR连接时,Encryption_Enabled可以是0x00、0x01、0x02
具体含义如下:
举例说明:
Encryption Change Event
0x04:表示HCI Event Packet
0x08:表示这个event是Encryption Change event
0x04:表示Parameter_Total_Length即4个byte
0x00:表示status为0
0x0003:表示Connection_Handle
0x01:表示使用E0方式进行连接层加密(BR/EDR)
(6)Change Connection Link Key Command
这个命令可以强制与Connection_Handle(ACL)相关的本地和远端设备产生一个新的link key
参数说明:
Connection_Handle(2 Octet):表示一个ACL连接
举例说明:(暂时没有)
(7)Change Connection Link Key Complete Event
这个event表示Change Connection Link Key Command完成
参数说明:
Status(1 Octet):0x00表示成功,其他值表示失败
Connection_Handle(2 Octet):表示一个ACL连接
举例说明:(暂时没有)