add hap_read_cb_t for HAPChar

This commit is contained in:
hathach committed 2018-02-06 19:53:33 +07:00
1 parent 9eb616e334
commit 153fafc353
6 files changed
+100 -78

No files matched your search

+1
View File
@@ -117,6 +117,7 @@ bool BLEHomekit::setAdv(BLEAdvertisingData& adv_ref)
// Full name is not added, added full name to scan response
if ( !adv.addName() )
{
Bluefruit.ScanResponse.clearData();
Bluefruit.ScanResponse.addName();
}
+4
View File
@@ -87,6 +87,10 @@ class BLEHomekit : public Advertisable
HAPPairing _pairing;
HAPLightBulb _lightbulb;
#if CFG_DEBUG
friend void test_homekit(void);
#endif
};
#endif /* BLEHOMEKIT_H_ */
+36 -14
View File
@@ -64,12 +64,14 @@ HAPCharacteristic::HAPCharacteristic(BLEUuid bleuuid, uint8_t format, uint16_t u
_hap_req = NULL;
_hap_reqlen = 0;
_tid = 0;
_hap_resp = NULL;
_hap_resplen = 0;
_hap_resplen_sent = 0;
_hap_wr_cb = NULL;
_hap_rd_cb = NULL;
// Need at least decent length for HAP Procedure
_max_len = 64;
@@ -95,6 +97,11 @@ void HAPCharacteristic::setHapWriteCallback(hap_write_cb_t fp)
_hap_wr_cb = fp;
}
void HAPCharacteristic::setHapReadCallback(hap_read_cb_t fp)
{
_hap_rd_cb = fp;
}
/**
* GATT Descriptor includes: Instance ID
* HAP Descriptor includes: HAP Properties, User String, Presentation Format,
@@ -186,7 +193,7 @@ uint16_t HAPCharacteristic::writeHapValue(uint32_t num)
return writeHapValue(&num, len);
}
void HAPCharacteristic::createHapResponse(uint16_t conn_hdl, uint8_t tid, uint8_t status, TLV8_t tlv_para[], uint8_t count)
void HAPCharacteristic::createHapResponse(uint16_t conn_hdl, uint8_t status, TLV8_t tlv_para[], uint8_t count)
{
// Determine body len (not including 2 byte length itself)
uint16_t body_len = tlv8_encode_calculate_len(tlv_para, count);
@@ -213,7 +220,7 @@ void HAPCharacteristic::createHapResponse(uint16_t conn_hdl, uint8_t tid, uint8_
varclr(&hap_resp->header.control);
hap_resp->header.control.fragment = 0;
hap_resp->header.control.type = HAP_PDU_RESPONSE;
hap_resp->header.tid = tid;
hap_resp->header.tid = _tid;
hap_resp->header.status = status;
hap_resp->body_len = body_len;
@@ -238,7 +245,7 @@ void HAPCharacteristic::createHapResponse(uint16_t conn_hdl, uint8_t tid, uint8_
memmove(dst+2, src, cc);
memcpy(&dst[0], &ctrl, 1);
dst[1] = tid;
dst[1] = _tid;
}
resp_len += nfrag*2;
@@ -286,25 +293,25 @@ void HAPCharacteristic::processChrSignatureRead(uint16_t conn_hdl, HAPRequest_t*
{ .type = HAP_PARAM_GATT_FORMAT_DESC , .len = sizeof(fmt_desc) , .value = &fmt_desc },
};
createHapResponse(conn_hdl, hap_req->header.tid, HAP_STATUS_SUCCESS, tlv_para, arrcount(tlv_para));
createHapResponse(conn_hdl, HAP_STATUS_SUCCESS, tlv_para, arrcount(tlv_para));
}
void HAPCharacteristic::processChrRead(uint16_t conn_hdl, HAPRequest_t* hap_req)
{
TLV8_t tlv_para = { .type = HAP_PARAM_VALUE, .len = _vallen, .value = _value };
createHapResponse(conn_hdl, hap_req->header.tid, HAP_STATUS_SUCCESS, &tlv_para, 1);
createHapResponse(conn_hdl, HAP_STATUS_SUCCESS, &tlv_para, 1);
}
void HAPCharacteristic::processHapRequest(uint16_t conn_hdl, HAPRequest_t* hap_req)
{
if (hap_req->header.control.type != HAP_PDU_REQUEST)
{
createHapResponse(conn_hdl, hap_req->header.tid, HAP_STATUS_UNSUPPORTED_PDU);
createHapResponse(conn_hdl, HAP_STATUS_UNSUPPORTED_PDU);
}
else if (hap_req->header.instance_id != _cid)
{
createHapResponse(conn_hdl, hap_req->header.tid, HAP_STATUS_INVALID_INSTANCE_ID);
createHapResponse(conn_hdl, HAP_STATUS_INVALID_INSTANCE_ID);
}else
{
LOG_LV2("HAP", "Recv %s request, TID = 0x%02X, CS_ID = 0x%04X", hap_opcode_str[hap_req->header.opcode], hap_req->header.tid, hap_req->header.instance_id);
@@ -324,7 +331,7 @@ void HAPCharacteristic::processHapRequest(uint16_t conn_hdl, HAPRequest_t* hap_r
_hap_wr_cb(conn_hdl, this, hap_req);
}else
{
createHapResponse(conn_hdl, hap_req->header.tid, HAP_STATUS_UNSUPPORTED_PDU);
createHapResponse(conn_hdl, HAP_STATUS_UNSUPPORTED_PDU);
}
break;
@@ -338,7 +345,7 @@ void HAPCharacteristic::processHapRequest(uint16_t conn_hdl, HAPRequest_t* hap_r
// case HAP_OPCODE_SVC_SIGNATURE_READ: break;
default:
createHapResponse(conn_hdl, hap_req->header.tid, HAP_STATUS_UNSUPPORTED_PDU);
createHapResponse(conn_hdl, HAP_STATUS_UNSUPPORTED_PDU);
break;
}
}
@@ -423,25 +430,31 @@ void HAPCharacteristic::processGattWrite(uint16_t conn_hdl, ble_gatts_evt_write_
_hap_reqlen += fraglen;
}
// Reply before processing request since cryptography take time and could cause timeout
VERIFY_STATUS( sd_ble_gatts_rw_authorize_reply(conn_hdl, &reply), );
// process when full request is received
if ( (_hap_reqlen == sizeof(HAPRequestHeader_t)) ||
(_hap_reqlen == _hap_req->body_len + sizeof(HAPRequestHeader_t) + 2) )
{
LOG_LV2_BUFFER("HAP Request", _hap_req, _hap_reqlen);
// save tid
_tid = _hap_req->header.tid;
processHapRequest(conn_hdl, _hap_req);
LOG_LV2("HAP", "Response: Control = 0x%02X, TID = 0x%02X, Status = %d", _hap_resp->header.control, _hap_resp->header.tid, _hap_resp->header.status);
LOG_LV2_BUFFER(NULL, _hap_resp, _hap_resplen);
if (_hap_resp )
{
LOG_LV2("HAP", "Response: Control = 0x%02X, TID = 0x%02X, Status = %d", _hap_resp->header.control, _hap_resp->header.tid, _hap_resp->header.status);
LOG_LV2_BUFFER(NULL, _hap_resp, _hap_resplen);
}
// Free if needed
if ( _hap_reqlen > gatt_mtu ) rtos_free(_hap_req);
_hap_req = NULL;
_hap_reqlen = 0;
}
// Reply before processing request since cryptography take time and could cause timeout
VERIFY_STATUS( sd_ble_gatts_rw_authorize_reply(conn_hdl, &reply), );
}
void HAPCharacteristic::processGattRead(uint16_t conn_hdl, ble_gatts_evt_read_t* gatt_req)
@@ -464,6 +477,15 @@ void HAPCharacteristic::processGattRead(uint16_t conn_hdl, ble_gatts_evt_read_t*
}
};
// If Response has not been generated, call the read callback
if ( _hap_resp == NULL && _hap_resplen == 0 )
{
if ( _hap_rd_cb )
{
_hap_rd_cb(conn_hdl, this);
}
}
if (_hap_resplen_sent < _hap_resplen)
{
uint16_t len = min16(_hap_resplen - _hap_resplen_sent, gatt_mtu);
+6 -1
View File
@@ -56,6 +56,7 @@ class HAPCharacteristic : public BLECharacteristic
{
public:
typedef void (*hap_write_cb_t) (uint16_t conn_hdl, HAPCharacteristic* chr, HAPRequest_t const* hap_req);
typedef void (*hap_read_cb_t ) (uint16_t conn_hdl, HAPCharacteristic* chr);
static BLEUuid _g_uuid_cid;
HAPCharacteristic(BLEUuid bleuuid, uint8_t format, uint16_t unit = UUID16_UNIT_UNITLESS);
@@ -70,11 +71,12 @@ class HAPCharacteristic : public BLECharacteristic
// Callbacks
void setHapWriteCallback(hap_write_cb_t fp);
void setHapReadCallback(hap_read_cb_t fp);
/*------------- Internal Functions -------------*/
virtual void _eventHandler(ble_evt_t* event);
void createHapResponse(uint16_t conn_hdl, uint8_t tid, uint8_t status, TLV8_t tlv_para[] = NULL, uint8_t count = 0);
void createHapResponse(uint16_t conn_hdl, uint8_t status, TLV8_t tlv_para[] = NULL, uint8_t count = 0);
private:
uint16_t _cid;
@@ -83,6 +85,7 @@ class HAPCharacteristic : public BLECharacteristic
// HAP request & response
HAPRequest_t* _hap_req;
uint16_t _hap_reqlen;
uint8_t _tid;
HAPResponse_t* _hap_resp;
uint16_t _hap_resplen;
@@ -94,6 +97,7 @@ class HAPCharacteristic : public BLECharacteristic
// Callbacks
hap_write_cb_t _hap_wr_cb;
hap_read_cb_t _hap_rd_cb;
err_t _addChrIdDescriptor(void);
@@ -104,6 +108,7 @@ class HAPCharacteristic : public BLECharacteristic
void processChrSignatureRead(uint16_t conn_hdl, HAPRequest_t* hap_req);
void processChrRead(uint16_t conn_hdl, HAPRequest_t* hap_req);
friend void test_homekit(void);
};
#endif /* HAPCHARACTERISTIC_H_ */
+47 -61
View File
@@ -43,46 +43,7 @@
#define DEBUG_HAP_PAIRING 0
/*
The following is a description of SRP-6 and 6a, the latest versions of SRP:
N A large safe prime (N = 2q+1, where q is prime)
All arithmetic is done modulo N.
g A generator modulo N
k Multiplier parameter (k = H(N, g) in SRP-6a, k = 3 for legacy SRP-6)
s User's salt
I Username
p Cleartext Password
H() One-way hash function
^ (Modular) Exponentiation
u Random scrambling parameter
a,b Secret ephemeral values
A,B Public ephemeral values
x Private key (derived from p and s)
v Password verifier
The host stores passwords using the following formula:
x = H(s, p) (s is chosen randomly)
v = g^x (computes password verifier)
The host then keeps {I, s, v} in its password database. The authentication protocol itself goes as follows:
User -> Host: I, A = g^a (identifies self, a = random number)
Host -> User: s, B = kv + g^b (sends salt, b = random number)
Both: u = H(A, B)
User: x = H(s, p) (user enters password)
User: S = (B - kg^x) ^ (a + ux) (computes session key)
User: K = H(S)
Host: S = (Av^u) ^ b (computes session key)
Host: K = H(S)
Now the two parties have a shared, strong session key K. To complete authentication, they need to prove to each other that their keys match. One possible way:
User -> Host: M = H(H(N) xor H(g), H(I), s, A, B, K)
Host -> User: H(A, M, K)
The two parties also employ the following safeguards:
The user will abort if he receives B == 0 (mod N) or u == 0.
The host will abort if it detects that A == 0 (mod N).
The user must show his proof of K first. If the server detects that the user's proof is incorrect, it must abort without showing its own proof of K.
/* SRP design detail http://srp.stanford.edu/design.html
*/
// kTLV type for pairing
@@ -141,7 +102,23 @@ static const char* pairing_type_str[] =
#endif
void _pairing_setup_write_cb (uint16_t conn_hdl, HAPCharacteristic* chr, HAPRequest_t const* hap_req);
void _pair_setup_write_cb (uint16_t conn_hdl, HAPCharacteristic* chr, HAPRequest_t const* hap_req);
// TODO add connection handle parameter
void gatt_reply_now(void)
{
ble_gatts_rw_authorize_reply_params_t reply =
{
.type = BLE_GATTS_AUTHORIZE_TYPE_WRITE,
.params = {
.write = {
.gatt_status = BLE_GATT_STATUS_SUCCESS,
.update = 1,
}
}
};
VERIFY_STATUS( sd_ble_gatts_rw_authorize_reply(Bluefruit.connHandle(), &reply), );
}
HAPPairing::HAPPairing(void)
: HAPService(HAP_UUID_SVC_PAIRING),
@@ -158,7 +135,7 @@ err_t HAPPairing::begin(void)
VERIFY_STATUS( HAPService::begin() ); // Invoke base class begin()
_setup.setHapProperties(HAP_CHR_PROPS_READ | HAP_CHR_PROPS_WRITE);
_setup.setHapWriteCallback(_pairing_setup_write_cb);
_setup.setHapWriteCallback(_pair_setup_write_cb);
_setup.setMaxLen(BLE_GATTS_VAR_ATTR_LEN_MAX);
VERIFY_STATUS( _setup.begin() );
@@ -181,7 +158,7 @@ err_t HAPPairing::begin(void)
return ERROR_NONE;
}
void HAPPairing::createSrpResponse(uint16_t conn_hdl, uint8_t tid, uint8_t status, TLV8_t ktlv[], uint8_t count)
void HAPPairing::createSrpResponse(uint16_t conn_hdl, uint8_t status, TLV8_t ktlv[], uint8_t count)
{
uint16_t srplen = tlv8_encode_calculate_len(ktlv, count);
uint8_t* srpbuf = (uint8_t*) rtos_malloc(srplen);
@@ -194,7 +171,7 @@ void HAPPairing::createSrpResponse(uint16_t conn_hdl, uint8_t tid, uint8_t statu
#endif
TLV8_t tlv = { .type = HAP_PARAM_VALUE, .len = srplen, .value = srpbuf };
_setup.createHapResponse(conn_hdl, tid, status, &tlv, 1);
_setup.createHapResponse(conn_hdl, status, &tlv, 1);
}
rtos_free(srpbuf);
@@ -229,7 +206,7 @@ void HAPPairing::pair_setup_m1(uint16_t conn_hdl, HAPRequest_t const* hap_req)
LOG_LV2_BUFFER("SRP Salt" , tlv_para[2].value, tlv_para[2].len);
#endif
createSrpResponse(conn_hdl, hap_req->header.tid, HAP_STATUS_SUCCESS, tlv_para, arrcount(tlv_para));
createSrpResponse(conn_hdl, HAP_STATUS_SUCCESS, tlv_para, arrcount(tlv_para));
}
void HAPPairing::pair_setup_m3(uint16_t conn_hdl, HAPRequest_t const* hap_req, TLV8_t pubkey, TLV8_t proof)
@@ -248,7 +225,7 @@ void HAPPairing::pair_setup_m3(uint16_t conn_hdl, HAPRequest_t const* hap_req, T
#endif
// Set iOS public key
srp_setA( (uint8_t*) pubkey.value, pubkey.len, NULL);
srp_setA( (uint8_t*) pubkey.value, pubkey.len, gatt_reply_now);
// Check proof
if ( srp_checkM1( (uint8_t*) proof.value, proof.len) )
@@ -271,7 +248,7 @@ void HAPPairing::pair_setup_m3(uint16_t conn_hdl, HAPRequest_t const* hap_req, T
#endif
}
createSrpResponse(conn_hdl, hap_req->header.tid, HAP_STATUS_SUCCESS, tlv_para, arrcount(tlv_para));
createSrpResponse(conn_hdl, HAP_STATUS_SUCCESS, tlv_para, arrcount(tlv_para));
}
void HAPPairing::pair_setup_m5(uint16_t conn_hdl, HAPRequest_t const* hap_req, TLV8_t encrypted)
@@ -287,6 +264,8 @@ void HAPPairing::pair_setup_m5(uint16_t conn_hdl, HAPRequest_t const* hap_req, T
bool failed = false;
uint32_t us = micros();
/*------------------------------------------------------------------*/
/* M5 Verification
*------------------------------------------------------------------*/
@@ -346,9 +325,9 @@ void HAPPairing::pair_setup_m5(uint16_t conn_hdl, HAPRequest_t const* hap_req, T
memcpy(message, signature, 64);
// Step 3: Derive iOSDeviceX
crypto_hkdf(message + 64, (uint8_t*) "Pair-Setup-Controller-Sign-Salt", 31, (uint8_t*) "Pair-Setup-Controller-Sign-Info\001", 32, srp_getK(), 64);\
crypto_hkdf(message + 64, (uint8_t*) "Pair-Setup-Controller-Sign-Salt", 31, (uint8_t*) "Pair-Setup-Controller-Sign-Info\001", 32, srp_getK(), 64);
// Step 4: Consutrct iOSDeviceInfo
// Step 4: Construct iOSDeviceInfo
memcpy(message + 64 + 32, client, 36);
memcpy(message + 64 + 32 + 36, ltpk, 32);
@@ -383,7 +362,7 @@ void HAPPairing::pair_setup_m5(uint16_t conn_hdl, HAPRequest_t const* hap_req, T
tlv_para[1].len = 1;
tlv_para[1].value = &pair_error;
createSrpResponse(conn_hdl, hap_req->header.tid, HAP_STATUS_SUCCESS, tlv_para, arrcount(tlv_para));
createSrpResponse(conn_hdl, HAP_STATUS_SUCCESS, tlv_para, arrcount(tlv_para));
return;
}
@@ -392,8 +371,11 @@ void HAPPairing::pair_setup_m5(uint16_t conn_hdl, HAPRequest_t const* hap_req, T
/* M6 Response Generation
*------------------------------------------------------------------*/
// FIXME change later
const uint8_t pairing_device_name[] = "41:42:43:44:45:46";
uint8_t pairing_devname_len = 17; // sizeof(pairing_device_name)-1; // excluding null termination
const uint8_t pairing_device_name[] = "19:07:60:29:4E:C3"; // must be the string representation of Device ID in advertising
enum { pairing_devname_len = 17 };
// const uint8_t pairing_device_name[] = "TTTFF972-040E-4915-A83D-A964690DDTTT";
// enum { pairing_devname_len = 36 };
uint8_t smessage[64 + 32 + pairing_devname_len + 32];
@@ -408,7 +390,10 @@ void HAPPairing::pair_setup_m5(uint16_t conn_hdl, HAPRequest_t const* hap_req, T
uint64_t slen = 0;
crypto_sign(smessage, &slen, smessage + 64, sizeof(smessage) - 64, crypto_keys.sign.secret);
// Step 5: Contruct sub-tlv
gatt_reply_now();
// Step 5: Construct sub-tlv
TLV8_t ktlv[] =
{
{ .type = PAIRING_TYPE_IDENTIFIER, .len = pairing_devname_len, .value = pairing_device_name },
@@ -416,28 +401,27 @@ void HAPPairing::pair_setup_m5(uint16_t conn_hdl, HAPRequest_t const* hap_req, T
{ .type = PAIRING_TYPE_SIGNATURE , .len = 64, .value = smessage }
};
// Additional 16 byte for Auth Tag
uint16_t const lbuffer = tlv8_encode_calculate_len(ktlv, arrcount(ktlv)) + 16;
uint8_t* buffer = (uint8_t*) rtos_malloc(lbuffer);
uint16_t const lbuffer = tlv8_encode_calculate_len(ktlv, arrcount(ktlv));
uint8_t* buffer = (uint8_t*) rtos_malloc(lbuffer + 16); // Additional 16 byte for Auth Tag
VERIFY( buffer != NULL, );
tlv8_encode_n(buffer, lbuffer-16, ktlv, arrcount(ktlv));
tlv8_encode_n(buffer, lbuffer, ktlv, arrcount(ktlv));
// Step 6: Encrypt above sub-tlv and generate 16-byte auth tag.
// uint8_t session_key[64]; declare above
// crypto_hkdf(session_key, (uint8_t*) "Pair-Setup-Encrypt-Salt", 23, (uint8_t*) "Pair-Setup-Encrypt-Info\001", 24, srp_getK(), 64);
crypto_encryptAndSeal(session_key, (uint8_t*) "PS-Msg06", buffer, lbuffer - 16, buffer, buffer + lbuffer - 16);
crypto_encryptAndSeal(session_key, (uint8_t*) "PS-Msg06", buffer, lbuffer, buffer, buffer + lbuffer);
tlv_para[1].type = PAIRING_TYPE_ENCRYPTED_DATA;
tlv_para[1].len = lbuffer;
tlv_para[1].len = lbuffer+16;
tlv_para[1].value = buffer;
createSrpResponse(conn_hdl, hap_req->header.tid, HAP_STATUS_SUCCESS, tlv_para, arrcount(tlv_para));
createSrpResponse(conn_hdl, HAP_STATUS_SUCCESS, tlv_para, arrcount(tlv_para));
rtos_free(buffer);
}
void _pairing_setup_write_cb (uint16_t conn_hdl, HAPCharacteristic* chr, HAPRequest_t const* hap_req)
void _pair_setup_write_cb (uint16_t conn_hdl, HAPCharacteristic* chr, HAPRequest_t const* hap_req)
{
uint16_t body_len = hap_req->body_len;
uint8_t const* body_data = hap_req->body_data;
@@ -526,6 +510,7 @@ void _pairing_setup_write_cb (uint16_t conn_hdl, HAPCharacteristic* chr, HAPRequ
break;
case 5:
svc.pair_setup_m5(conn_hdl, hap_req, iencrypted);
tlv8_decode_cleanup(iencrypted);
@@ -543,3 +528,4 @@ void _pairing_setup_write_cb (uint16_t conn_hdl, HAPCharacteristic* chr, HAPRequ
tlv8_decode_cleanup(tlv);
}
}
@@ -55,9 +55,13 @@ class HAPPairing : public HAPService
void pair_setup_m3(uint16_t conn_hdl, HAPRequest_t const* hap_req, TLV8_t pubkey, TLV8_t proof);
void pair_setup_m5(uint16_t conn_hdl, HAPRequest_t const* hap_req, TLV8_t encrypted);
void createSrpResponse(uint16_t conn_hdl, uint8_t tid, uint8_t status, TLV8_t ktlv[], uint8_t count);
void createSrpResponse(uint16_t conn_hdl, uint8_t status, TLV8_t ktlv[], uint8_t count);
friend void _pairing_setup_write_cb (uint16_t conn_hdl, HAPCharacteristic* chr, HAPRequest_t const* hap_req);
friend void _pair_setup_write_cb (uint16_t conn_hdl, HAPCharacteristic* chr, HAPRequest_t const* hap_req);
#if CFG_DEBUG
friend void test_homekit(void);
#endif
};