From 5a356432b3e1cd3a5718a1b7d16b02c781b3a5bb Mon Sep 17 00:00:00 2001 From: Gregg Date: Sun, 30 Jul 2023 21:54:14 -0500 Subject: [PATCH] Simplified `uint8_t *TLV::buf(tagType tag, int len)` Also updated HAP.cpp to use new `uint8_t *TLV::buf(tagType tag, uint8_t *src, int len)` --- src/HAP.cpp | 12 ++++++------ src/TLV.h | 18 ++++-------------- 2 files changed, 10 insertions(+), 20 deletions(-) diff --git a/src/HAP.cpp b/src/HAP.cpp index 57d474a..0947103 100644 --- a/src/HAP.cpp +++ b/src/HAP.cpp @@ -609,8 +609,8 @@ int HAPClient::postPairSetupURL(){ crypto_sign_detached(tlv8.buf(kTLVType_Signature,64),NULL,accessoryInfo,accessoryInfoLen,accessory.LTSK); // produce signature of accessoryInfo using AccessoryLTSK (Ed25519 long-term secret key) - memcpy(tlv8.buf(kTLVType_Identifier,accessoryPairingIDLen),accessoryPairingID,accessoryPairingIDLen); // set Identifier TLV record as accessoryPairingID - memcpy(tlv8.buf(kTLVType_PublicKey,accessoryLTPKLen),accessoryLTPK,accessoryLTPKLen); // set PublicKey TLV record as accessoryLTPK + tlv8.buf(kTLVType_Identifier,accessoryPairingID,accessoryPairingIDLen); // set Identifier TLV record as accessoryPairingID + tlv8.buf(kTLVType_PublicKey,accessoryLTPK,accessoryLTPKLen); // set PublicKey TLV record as accessoryLTPK LOG2("------- ENCRYPTING SUB-TLVS -------\n"); @@ -722,7 +722,7 @@ int HAPClient::postPairVerifyURL(){ crypto_sign_detached(tlv8.buf(kTLVType_Signature,64),NULL,accessoryInfo,accessoryInfoLen,accessory.LTSK); // produce signature of accessoryInfo using AccessoryLTSK (Ed25519 long-term secret key) - memcpy(tlv8.buf(kTLVType_Identifier,accessoryPairingIDLen),accessoryPairingID,accessoryPairingIDLen); // set Identifier TLV record as accessoryPairingID + tlv8.buf(kTLVType_Identifier,accessoryPairingID,accessoryPairingIDLen); // set Identifier TLV record as accessoryPairingID LOG2("------- ENCRYPTING SUB-TLVS -------\n"); @@ -744,9 +744,9 @@ int HAPClient::postPairVerifyURL(){ LOG2("---------- END SUB-TLVS! ----------\n"); - tlv8.buf(kTLVType_EncryptedData,edLen); // set length of EncryptedData TLV record, which should now include the Authentication Tag at the end as required by HAP - tlv8.val(kTLVType_State,pairState_M2); // set State= - memcpy(tlv8.buf(kTLVType_PublicKey,32),publicCurveKey,32); // set PublicKey to Accessory's Curve25519 public key + tlv8.buf(kTLVType_EncryptedData,edLen); // set length of EncryptedData TLV record, which should now include the Authentication Tag at the end as required by HAP + tlv8.val(kTLVType_State,pairState_M2); // set State= + tlv8.buf(kTLVType_PublicKey,publicCurveKey,32); // set PublicKey to Accessory's Curve25519 public key tlvRespond(); // send response to client return(1); diff --git a/src/TLV.h b/src/TLV.h index 6bca6db..9890b54 100644 --- a/src/TLV.h +++ b/src/TLV.h @@ -178,19 +178,7 @@ uint8_t *TLV::buf(tagType tag){ template uint8_t *TLV::buf(tagType tag, int len){ - tlv_t *tlv=find(tag); - - if(tlv && len<=tlv->maxLen){ - tlv->len=len; - cLen+=tlv->len; - - for(int i=0;ilen;i+=255) - cLen+=2; - - return(tlv->val); - } - - return(NULL); + return(buf(tag,NULL,len)); } ////////////////////////////////////// @@ -208,7 +196,9 @@ uint8_t *TLV::buf(tagType tag, uint8_t *src, int len){ for(int i=0;ilen;i+=255) cLen+=2; - memcpy(tlv->val,src,len); + if(src) + memcpy(tlv->val,src,len); + return(tlv->val); }