【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:
c 复制代码
    /* 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:
c 复制代码
    /* 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));
相关推荐
风清已存在6 小时前
阿里云OSS与IOT使用详解
物联网·阿里云·云计算
YesPMP平台官方账号9 小时前
AI+教育|拥抱AI智能科技,让课堂更生动高效
人工智能·科技·物联网·ai·智慧校园·教育
shiming887911 小时前
物联网智能项目
物联网
bigbig猩猩11 小时前
物联网智能项目
物联网
神一样的老师11 小时前
人工智能物联网的去中心化和分布式学习:全面综述、新兴挑战和机遇
人工智能·物联网·去中心化
hong16168811 小时前
物联网智能项目
物联网
MGT_979620 小时前
ESP01的AT指令连接到阿里云平台
嵌入式硬件·物联网·阿里云·云计算
神一样的老师21 小时前
人工智能物联网:一项综述
人工智能·物联网
北京天拓四方1 天前
边缘计算智能网关的功能应用与优势-天拓四方
物联网·其他·边缘计算·iot
钡铼技术1 天前
通过HTforWeb在ARMxy边缘计算网关上实现工业互联网
人工智能·物联网·边缘计算·钡铼技术·armxy边缘计算网关