Skip to content

Commit 750b7f1

Browse files
committed
Actually check remote DHKey
1 parent 0aa2e87 commit 750b7f1

File tree

2 files changed

+58
-13
lines changed

2 files changed

+58
-13
lines changed

src/utility/HCI.cpp

+29-6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#include "btct.h"
2525
#include "HCI.h"
2626

27+
//#define _BLE_TRACE_
28+
2729
#define HCI_COMMAND_PKT 0x01
2830
#define HCI_ACLDATA_PKT 0x02
2931
#define HCI_EVENT_PKT 0x04
@@ -1408,13 +1410,34 @@ void HCIClass::handleEventPkt(uint8_t /*plen*/, uint8_t pdata[])
14081410
btct.printBytes(MasterIOCap, 3);
14091411
Serial.println("Send Eb Back.");
14101412
#endif
1411-
uint8_t ret[17];
1412-
ret[0] = 0x0d;
1413-
for(int i=0; i<sizeof(Eb); i++){
1414-
ret[sizeof(Eb)-i] = Eb[i];
1413+
// Check if RemoteDHKeyCheck = Ea
1414+
bool EaCheck = true;
1415+
for(int i = 0; i < 16; i++){
1416+
if (Ea[i] != HCI.remoteDHKeyCheckBuffer[i]){
1417+
EaCheck = false;
1418+
}
1419+
}
1420+
1421+
if (EaCheck){
1422+
// Send our confirmation value to complete authentication stage 2
1423+
uint8_t ret[17];
1424+
ret[0] = CONNECTION_PAIRING_DHKEY_CHECK;
1425+
for(int i=0; i<sizeof(Eb); i++){
1426+
ret[sizeof(Eb)-i] = Eb[i];
1427+
}
1428+
HCI.sendAclPkt(connectionHandle, SECURITY_CID, sizeof(ret), ret );
1429+
ATT.setPeerEncryption(connectionHandle, encryption | PEER_ENCRYPTION::SENT_DH_CHECK);
1430+
1431+
} else {
1432+
// If check fails, abort
1433+
#ifdef _BLE_TRACE_
1434+
Serial.println("Error: DHKey check failed - Aborting");
1435+
#endif
1436+
uint8_t ret[2] = {CONNECTION_PAIRING_FAILED, 0x0B}; // DHKey Check Faile
1437+
HCI.sendAclPkt(connectionHandle, SECURITY_CID, sizeof(ret), ret);
1438+
ATT.setPeerEncryption(connectionHandle, NO_ENCRYPTION);
14151439
}
1416-
HCI.sendAclPkt(connectionHandle, 0x06, sizeof(ret), ret );
1417-
ATT.setPeerEncryption(connectionHandle, encryption | PEER_ENCRYPTION::SENT_DH_CHECK);
1440+
14181441
}else{
14191442
#ifdef _BLE_TRACE_
14201443
Serial.println("Waiting on other DHKey check before calculating.");

src/utility/L2CAPSignaling.cpp

+29-7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#define CONNECTION_PARAMETER_UPDATE_REQUEST 0x12
2727
#define CONNECTION_PARAMETER_UPDATE_RESPONSE 0x13
2828

29+
//#define _BLE_TRACE_
30+
2931
L2CAPSignalingClass::L2CAPSignalingClass() :
3032
_minInterval(0),
3133
_maxInterval(0),
@@ -353,14 +355,34 @@ void L2CAPSignalingClass::handleSecurityData(uint16_t connectionHandle, uint8_t
353355
btct.printBytes(MasterIOCap, 3);
354356
Serial.println("Send Eb Back.");
355357
#endif
356-
357-
uint8_t ret[17];
358-
ret[0] = 0x0d;
359-
for(int i=0; i<sizeof(Eb); i++){
360-
ret[sizeof(Eb)-i] = Eb[i];
358+
359+
// Check if RemoteDHKeyCheck = Ea
360+
bool EaCheck = true;
361+
for(int i = 0; i < 16; i++){
362+
if (Ea[i] != RemoteDHKeyCheck[i]){
363+
EaCheck = false;
364+
}
365+
}
366+
367+
if (EaCheck){
368+
// Send our confirmation value to complete authentication stage 2
369+
uint8_t ret[17];
370+
ret[0] = CONNECTION_PAIRING_DHKEY_CHECK;
371+
for(int i=0; i<sizeof(Eb); i++){
372+
ret[sizeof(Eb)-i] = Eb[i];
373+
}
374+
HCI.sendAclPkt(connectionHandle, SECURITY_CID, sizeof(ret), ret );
375+
ATT.setPeerEncryption(connectionHandle, encryptionState | PEER_ENCRYPTION::SENT_DH_CHECK);
376+
377+
} else {
378+
// If check fails, abort
379+
#ifdef _BLE_TRACE_
380+
Serial.println("Error: DHKey check failed - Aborting");
381+
#endif
382+
uint8_t ret[2] = {CONNECTION_PAIRING_FAILED, 0x0B}; // DHKey Check Faile
383+
HCI.sendAclPkt(connectionHandle, SECURITY_CID, sizeof(ret), ret);
384+
ATT.setPeerEncryption(connectionHandle, NO_ENCRYPTION);
361385
}
362-
HCI.sendAclPkt(connectionHandle, 0x06, sizeof(ret), ret );
363-
ATT.setPeerEncryption(connectionHandle, encryptionState | PEER_ENCRYPTION::SENT_DH_CHECK);
364386
}
365387
}
366388
}

0 commit comments

Comments
 (0)