【BLE】四.SMP安全配对详解

设备配对流程

在这里插入图片描述

SMP专业术语

  • Paring(配对):
    配对能力交换,设备认证,密钥生成,连接加密以及机密信息分发等
    过程

  • Bonding(绑定)
    配对中会生成一个长期密钥(LTK,long-term Key),双方把LTK存储在Flash,那么这两个设备再次重连就可跳过配对流程,且直接使用LTK对蓝牙连接进行加密;
    不存储LTK(不分发LTK),paring完成后连接也是加密的,但重连需再次进行paring流程,否则两者还是明文通信;
    在不引起误解的情况下,我们经常把paring当成paring和bonding两者的组合,因为只paring不bonding的应用情况非常少见,下文就不区分paring和bonding的区别。

  • SM(security manager):
    蓝牙协议栈的安全管理层,包括paring,bonding,以及SMP。

  • SMP(security manager protocol)
    安全管理协议,两个设备之间的蓝牙交互命令序列,对paring的空中包进行了严格时序规定

  • MITM(man in the middle):
    A和B通信过程中,C会插入进来以模拟A或者B,并且具备截获和篡改A和B之间所有通信报文的能力;若需要具备MITM保护能力,通过认证实现,分别为:OOB认证信息,passkey以及numeric comparison。

  • OOB(out of band,带外认证):
    不通过蓝牙射频本身来交互,而是-通过如人眼,NFC,UART等带外方式来交互配对信息。

  • Passkey(pin码):
    用户在键盘中输入的6位数字,以达到认证设备的目的。

  • Numeric comparison(数字比较):
    跟passkey一样,也是用来认证设备的6位数字,显示在显示器上。如下:
    在这里插入图片描述

  • IO capabilities(输入输出能力):
    是否有键盘,是否有显示器。

  • IRK(Identity Resolving Key,蓝牙设备地址解析密钥):
    可解析的随机地址,比如iPhone手机,由于其地址随着时间会变化IRK通过解析变化的地址的规律,从而确定这些地址是否来自同一个设备,即IRK可以用来识别蓝牙设备身份;
    IRK一般由设备出厂时按照一定要求自动生成

  • Identity Address(设备唯一地址):
    包括publicrandom staticprivate resolvablerandom unresolved共四类;
    设备不支持privacy,那么identity address就等于public或者random static设备地址;。如果设备支持privacy,即使用private resolvable蓝牙设备地址,在这种情况下,虽然其地址每隔一段时间会变化一次,但是identity address仍然保持不变,其取值还是等于内在的public或者random static设备地址。
    Identity Address和IRK都可以用来唯一标识一个蓝牙设备。

  • TK(Temporary Key,临时密钥)

  • STK(short term key,短期密钥)

  • LTK(long term key,长期密钥)

IO能力

在这里插入图片描述

配对方式

在这里插入图片描述

只要有一端支持OOB,即使用OOB验证配对;只有都不支持才执行中间人(MITM)设置,即看两端是否有其一是支持使用IO功能的,支持就使用IO功能验证配对,如下所示:

IO功能选择

例如手机端作为发起端,一般都是“Keyboard Only”与“KeyBoard Display”,如下所示:
在这里插入图片描述
其中的无认证:
在这里插入图片描述

示例工程:

在这里插入图片描述

SMP相关初始化

  • 初始化流程:
    在这里插入图片描述

  • 设置安全配置参数:
    在这里插入图片描述

  • 服务端GATT回调:
    在这里插入图片描述

  • 服务端GAP回调:
    在这里插入图片描述

  • 客户端GAP回调:
    在这里插入图片描述

安全配置参数的代码片段

  • GATTS:
    /* set the security iocap & auth_req & key size & init key response key parameters to the stack*/
	
//	esp_ble_auth_req_t auth_req = ESP_LE_AUTH_NO_BOND;			//未启用绑定
//	esp_ble_auth_req_t auth_req = ESP_LE_AUTH_BOND;				//启用绑定
//	esp_ble_auth_req_t auth_req = ESP_LE_AUTH_REQ_MITM;			//开启MITM保护
//	esp_ble_auth_req_t auth_req = ESP_LE_AUTH_REQ_SC_ONLY;		//未启用绑定的安全连接
//	esp_ble_auth_req_t auth_req = ESP_LE_AUTH_REQ_SC_BOND;		//启用绑定后的安全连接
	esp_ble_auth_req_t auth_req = ESP_LE_AUTH_REQ_SC_MITM;		//使用MITM保护和未启用连接的安全连接
//	esp_ble_auth_req_t auth_req = ESP_LE_AUTH_REQ_SC_MITM_BOND;	//安全连接,启用MITM保护和连接
	
//  esp_ble_io_cap_t iocap = ESP_IO_CAP_NONE;           //NoInputNoOutput
	esp_ble_io_cap_t iocap = ESP_IO_CAP_KBDISP;			//Keyboard display
//	esp_ble_io_cap_t iocap = ESP_IO_CAP_IO;				//DisplayYesNo
//	esp_ble_io_cap_t iocap = ESP_IO_CAP_OUT;			//DisplayOnly
//	esp_ble_io_cap_t iocap = ESP_IO_CAP_IN;				//KeyboardOnly

    uint8_t key_size = 16;      //the key size should be 7~16 bytes
    //启动秘钥通过LTK方式加密
    uint8_t init_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK;
    //响应秘钥通过IRK方式加密  
    uint8_t rsp_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK;
    //set static passkey
    uint32_t passkey = 123456;
    uint8_t auth_option = ESP_BLE_ONLY_ACCEPT_SPECIFIED_AUTH_DISABLE;
//	uint8_t auth_option = ESP_BLE_ONLY_ACCEPT_SPECIFIED_AUTH_ENABLE;			//必须绑定才能开启
	
    uint8_t oob_support = ESP_BLE_OOB_DISABLE;			//关闭OOB
//	uint8_t oob_support = ESP_BLE_OOB_ENABLE;			//开启OOB

    esp_ble_gap_set_security_param(ESP_BLE_SM_SET_STATIC_PASSKEY, &passkey, sizeof(uint32_t));
    esp_ble_gap_set_security_param(ESP_BLE_SM_AUTHEN_REQ_MODE, &auth_req, sizeof(uint8_t));
    esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &iocap, sizeof(uint8_t));
    esp_ble_gap_set_security_param(ESP_BLE_SM_MAX_KEY_SIZE, &key_size, sizeof(uint8_t));
    esp_ble_gap_set_security_param(ESP_BLE_SM_ONLY_ACCEPT_SPECIFIED_SEC_AUTH, &auth_option, sizeof(uint8_t));
    esp_ble_gap_set_security_param(ESP_BLE_SM_OOB_SUPPORT, &oob_support, sizeof(uint8_t));
    /* If your BLE device acts as a Slave, the init_key means you hope which types of key of the master should distribute to you,
    and the response key means which key you can distribute to the master;
    If your BLE device acts as a master, the response key means you hope which types of key of the slave should distribute to you,
    and the init key means which key you can distribute to the slave. */
    esp_ble_gap_set_security_param(ESP_BLE_SM_SET_INIT_KEY, &init_key, sizeof(uint8_t));
    esp_ble_gap_set_security_param(ESP_BLE_SM_SET_RSP_KEY, &rsp_key, sizeof(uint8_t));

  • GATTC:
    /* set the security iocap & auth_req & key size & init key response key parameters to the stack*/
	
//	esp_ble_auth_req_t auth_req = ESP_LE_AUTH_NO_BOND;			//未启用绑定
//	esp_ble_auth_req_t auth_req = ESP_LE_AUTH_BOND;				//启用绑定
//	esp_ble_auth_req_t auth_req = ESP_LE_AUTH_REQ_MITM;			//开启MITM保护
//	esp_ble_auth_req_t auth_req = ESP_LE_AUTH_REQ_SC_ONLY;		//未启用绑定的安全连接
//	esp_ble_auth_req_t auth_req = ESP_LE_AUTH_REQ_SC_BOND;		//启用绑定后的安全连接
	esp_ble_auth_req_t auth_req = ESP_LE_AUTH_REQ_SC_MITM;		//使用MITM保护和未启用连接的安全连接
//	esp_ble_auth_req_t auth_req = ESP_LE_AUTH_REQ_SC_MITM_BOND;	//安全连接,启用MITM保护和连接
	
//  esp_ble_io_cap_t iocap = ESP_IO_CAP_NONE;           //NoInputNoOutput
	esp_ble_io_cap_t iocap = ESP_IO_CAP_KBDISP;			//Keyboard display
//	esp_ble_io_cap_t iocap = ESP_IO_CAP_IO;				//DisplayYesNo
//	esp_ble_io_cap_t iocap = ESP_IO_CAP_OUT;			//DisplayOnly
//	esp_ble_io_cap_t iocap = ESP_IO_CAP_IN;				//KeyboardOnly



    uint8_t key_size = 16;      //the key size should be 7~16 bytes
    //启动秘钥通过LTK方式加密
    uint8_t init_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK;
    //响应秘钥通过IRK方式加密
    uint8_t rsp_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK;
//    uint8_t oob_support = ESP_BLE_OOB_DISABLE;		//关闭OOB
	uint8_t oob_support = ESP_BLE_OOB_ENABLE;			//开启OOB

    esp_ble_gap_set_security_param(ESP_BLE_SM_AUTHEN_REQ_MODE, &auth_req, sizeof(uint8_t));
    esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &iocap, sizeof(uint8_t));
    esp_ble_gap_set_security_param(ESP_BLE_SM_MAX_KEY_SIZE, &key_size, sizeof(uint8_t));
    esp_ble_gap_set_security_param(ESP_BLE_SM_OOB_SUPPORT, &oob_support, sizeof(uint8_t));
    /* If your BLE device act as a Slave, the init_key means you hope which types of key of the master should distribute to you,
    and the response key means which key you can distribute to the Master;
    If your BLE device act as a master, the response key means you hope which types of key of the slave should distribute to you,
    and the init key means which key you can distribute to the slave. */
    esp_ble_gap_set_security_param(ESP_BLE_SM_SET_INIT_KEY, &init_key, sizeof(uint8_t));
    esp_ble_gap_set_security_param(ESP_BLE_SM_SET_RSP_KEY, &rsp_key, sizeof(uint8_t));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值