Skip to content

Commit 7ecf299

Browse files
author
unknownconstant
authored
Merge pull request #5 from unknownconstant/pairing
Android bugfix
2 parents 6b56e82 + 6ef59a5 commit 7ecf299

File tree

4 files changed

+39
-31
lines changed

4 files changed

+39
-31
lines changed

examples/Peripheral/EncryptedBatteryMonitor/EncryptedBatteryMonitor.ino

+26-24
Original file line numberDiff line numberDiff line change
@@ -44,34 +44,34 @@ void setup() {
4444

4545
// IRKs are keys that identify the true owner of a random mac address.
4646
// Add IRKs of devices you are bonded with.
47-
BLE.setGetIRKs([](uint8_t* nIRKs, uint8_t** BADDR_TYPES, uint8_t*** BDAddrs, uint8_t*** IRKs){
47+
BLE.setGetIRKs([](uint8_t* nIRKs, uint8_t** BDaddrTypes, uint8_t*** BDAddrs, uint8_t*** IRKs){
4848
// Set to number of devices
4949
*nIRKs = 2;
5050

5151
*BDAddrs = new uint8_t*[*nIRKs];
5252
*IRKs = new uint8_t*[*nIRKs];
53-
*BADDR_TYPES = new uint8_t[*nIRKs];
53+
*BDaddrTypes = new uint8_t[*nIRKs];
5454

5555
// Set these to the mac and IRK for your bonded devices as printed in the serial console after bonding.
56-
uint8_t iPhoneMac [6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
57-
uint8_t iPhoneIRK[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
56+
uint8_t device1Mac[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
57+
uint8_t device1IRK[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
5858

59-
uint8_t iPadMac[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
60-
uint8_t iPadIRK[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, };
59+
uint8_t device2Mac[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
60+
uint8_t device2IRK[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
6161

6262

63-
(*BADDR_TYPES)[0] = 0;
64-
(*IRKs)[0] = new uint8_t[16];
65-
memcpy((*IRKs)[0],iPhoneIRK,16);
63+
(*BDaddrTypes)[0] = 0; // Type 0 is for pubc address, type 1 is for static random
6664
(*BDAddrs)[0] = new uint8_t[6];
67-
memcpy((*BDAddrs)[0], iPhoneMac, 6);
65+
(*IRKs)[0] = new uint8_t[16];
66+
memcpy((*IRKs)[0] , device1IRK,16);
67+
memcpy((*BDAddrs)[0], device1Mac, 6);
6868

6969

70-
(*BADDR_TYPES)[1] = 0;
71-
(*IRKs)[1] = new uint8_t[16];
72-
memcpy((*IRKs)[1],iPadIRK,16);
70+
(*BDaddrTypes)[1] = 0;
7371
(*BDAddrs)[1] = new uint8_t[6];
74-
memcpy((*BDAddrs)[1], iPadMac, 6);
72+
(*IRKs)[1] = new uint8_t[16];
73+
memcpy((*IRKs)[1] , device2IRK,16);
74+
memcpy((*BDAddrs)[1], device2Mac, 6);
7575

7676

7777
return 1;
@@ -83,17 +83,18 @@ void setup() {
8383
btct.printBytes(address,6);
8484

8585
// Set these to the MAC and LTK of your devices after bonding.
86-
uint8_t iPhoneMac [6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
87-
uint8_t iPhoneLTK[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
88-
uint8_t iPadMac [6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
89-
uint8_t iPadLTK[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
86+
uint8_t device1Mac[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
87+
uint8_t device1LTK[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
88+
uint8_t device2Mac[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
89+
uint8_t device2LTK[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
9090

9191

92-
if(memcmp(iPhoneMac, address, 6)==0){
93-
memcpy(LTK, iPhoneLTK, 16);
92+
if(memcmp(device1Mac, address, 6) == 0) {
93+
memcpy(LTK, device1LTK, 16);
94+
return 1;
95+
}else if(memcmp(device2Mac, address, 6) == 0) {
96+
memcpy(LTK, device2LTK, 16);
9497
return 1;
95-
}else if(memcmp(iPadMac, address, 6)==0){
96-
memcpy(LTK, iPadLTK, 16);
9798
}
9899
return 0;
99100
});
@@ -112,7 +113,8 @@ void setup() {
112113
return 1;
113114
});
114115

115-
while(1){// begin initialization
116+
while(1){
117+
// begin initialization
116118
if (!BLE.begin()) {
117119
Serial.println("starting BLE failed!");
118120
delay(200);
@@ -135,7 +137,7 @@ void setup() {
135137
batteryService.addCharacteristic(stringcharacteristic);
136138
batteryService.addCharacteristic(secretValue);
137139

138-
BLE.addService(batteryService); // Add the battery service
140+
BLE.addService(batteryService); // Add the battery service
139141
batteryLevelChar.writeValue(oldBatteryLevel); // set initial value for this characteristic
140142
char* stringCharValue = new char[32];
141143
stringCharValue = "string";

src/utility/ATT.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,10 @@ void ATTClass::addConnection(uint16_t handle, uint8_t role, uint8_t peerBdaddrTy
260260
Serial.println("Found match.");
261261
#endif
262262
}else{
263-
memset(_peers[peerIndex].resolvedAddress, 0, 6);
263+
#ifdef _BLE_TRACE_
264+
Serial.println("No matching MAC");
265+
#endif
266+
memset(&_peers[peerIndex].resolvedAddress, 0, 6);
264267
}
265268

266269
if (_eventHandlers[BLEConnected]) {

src/utility/HCI.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -1218,10 +1218,11 @@ void HCIClass::handleEventPkt(uint8_t /*plen*/, uint8_t pdata[])
12181218
uint16_t minLength;
12191219
uint16_t maxLength;
12201220
} remoteConnParamReqReply;
1221-
memcpy(&remoteConnParamReqReply, &remoteConnParamReq->connectionHandle, sizeof(remoteConnParamReq-1));
1221+
memcpy(&remoteConnParamReqReply, &remoteConnParamReq->connectionHandle, sizeof(RemoteConnParamReq)-1);
1222+
12221223
remoteConnParamReqReply.minLength = 0x000F;
12231224
remoteConnParamReqReply.maxLength = 0x0FFF;
1224-
sendCommand(OGF_LE_CTL << 10 | 0x20, sizeof(remoteConnParamReqReply), &remoteConnParamReqReply);
1225+
sendCommand(OGF_LE_CTL << 10 | 0x20, sizeof(RemoteConnParamReqReply), &remoteConnParamReqReply);
12251226
break;
12261227
}
12271228
case READ_LOCAL_P256_COMPLETE:{
@@ -1381,7 +1382,7 @@ int HCIClass::leEncrypt(uint8_t* key, uint8_t* plaintext, uint8_t* status, uint8
13811382
leEncryptCommand.plaintext[15-i] = plaintext[i];
13821383
}
13831384

1384-
int res = sendCommand(OGF_LE_CTL << 10 | LE_COMMAND::ENCRYPT, sizeof(leEncryptCommand), &leEncryptCommand);
1385+
int res = sendCommand(OGF_LE_CTL << 10 | LE_COMMAND::ENCRYPT, 32, &leEncryptCommand);
13851386
if(res == 0){
13861387
#ifdef _BLE_TRACE_
13871388
Serial.print("Copying from command Response length: ");

src/utility/L2CAPSignaling.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ void L2CAPSignalingClass::handleSecurityData(uint16_t connectionHandle, uint8_t
144144
ATT.localKeyDistribution = KeyDistribution(pairingRequest->responderKeyDistribution);
145145
KeyDistribution rkd(pairingRequest->responderKeyDistribution);
146146
AuthReq req(pairingRequest->authReq);
147+
KeyDistribution responseKD = KeyDistribution();
148+
responseKD.setIdKey(true);
147149
#ifdef _BLE_TRACE_
148150
Serial.print("Req has properties: ");
149151
Serial.print(req.Bonding()?"bonding, ":"no bonding, ");
@@ -171,7 +173,7 @@ void L2CAPSignalingClass::handleSecurityData(uint16_t connectionHandle, uint8_t
171173
uint8_t maxEncSize;
172174
uint8_t initiatorKeyDistribution;
173175
uint8_t responderKeyDistribution;
174-
} response = { CONNECTION_PAIRING_RESPONSE, LOCAL_IOCAP, 0, LOCAL_AUTHREQ, 0x10, 0b1011, 0b1011};
176+
} response = { CONNECTION_PAIRING_RESPONSE, LOCAL_IOCAP, 0, LOCAL_AUTHREQ, 0x10, responseKD.getOctet(), responseKD.getOctet()};
175177

176178
HCI.sendAclPkt(connectionHandle, SECURITY_CID, sizeof(response), &response);
177179
}
@@ -316,9 +318,9 @@ void L2CAPSignalingClass::handleSecurityData(uint16_t connectionHandle, uint8_t
316318
Serial.println("Calculate f5, f6:");
317319
Serial.print("DH : ");
318320
btct.printBytes(HCI.DHKey,32);
319-
Serial.println("Na : ");
321+
Serial.print("Na : ");
320322
btct.printBytes(HCI.Na,16);
321-
Serial.println("Nb : ");
323+
Serial.print("Nb : ");
322324
btct.printBytes(HCI.Nb,16);
323325
Serial.print("MAC : ");
324326
btct.printBytes(MacKey,16);

0 commit comments

Comments
 (0)