Revert "Changed TLV:add() so it returns TLV8 instead of iterator."

This reverts commit 7a50479bac.
This commit is contained in:
Gregg 2024-04-13 22:00:37 -05:00
parent 7a50479bac
commit 28990d6ed6
3 changed files with 30 additions and 27 deletions

View File

@ -370,6 +370,7 @@ int HAPClient::postPairSetupURL(uint8_t *content, size_t len){
return(0); return(0);
}; };
auto itPublicKey=responseTLV.add(kTLVType_PublicKey,384,NULL); // create blank PublicKey TLV with space for 384 bytes
if(srp==NULL) // create instance of SRP (if not already created) to persist until Pairing-Setup M5 completes if(srp==NULL) // create instance of SRP (if not already created) to persist until Pairing-Setup M5 completes
srp=new SRP6A; srp=new SRP6A;
@ -380,8 +381,7 @@ int HAPClient::postPairSetupURL(uint8_t *content, size_t len){
responseTLV.add(kTLVType_Salt,16,verifyData.get()->salt); // write Salt from verification data into TLV responseTLV.add(kTLVType_Salt,16,verifyData.get()->salt); // write Salt from verification data into TLV
responseTLV.add(kTLVType_PublicKey,384,NULL); // create blank PublicKey TLV with space for 384 bytes srp->createPublicKey(verifyData,*itPublicKey); // create accessory Public Key from stored verification data and write result into PublicKey TLV
srp->createPublicKey(verifyData,responseTLV.back()); // create accessory Public Key from stored verification data and write result into PublicKey TLV
tlvRespond(responseTLV); // send response to client tlvRespond(responseTLV); // send response to client
pairStatus=pairState_M3; // set next expected pair-state request from client pairStatus=pairState_M3; // set next expected pair-state request from client
@ -414,8 +414,9 @@ int HAPClient::postPairSetupURL(uint8_t *content, size_t len){
return(0); return(0);
}; };
responseTLV.add(kTLVType_Proof,64,NULL); // create blank accessory Proof TLV with space for 64 bytes auto itAccProof=responseTLV.add(kTLVType_Proof,64,NULL); // create blank accessory Proof TLV with space for 64 bytes
srp->createAccProof(responseTLV.back()); // M1 has been successully verified; now create accessory Proof M2
srp->createAccProof(*itAccProof); // M1 has been successully verified; now create accessory Proof M2
tlvRespond(responseTLV); // send response to client tlvRespond(responseTLV); // send response to client
pairStatus=pairState_M5; // set next expected pair-state request from client pairStatus=pairState_M5; // set next expected pair-state request from client
@ -512,13 +513,14 @@ int HAPClient::postPairSetupURL(uint8_t *content, size_t len){
TempBuffer<uint8_t> accessoryInfo(accessoryX,accessoryX.len(),accessory.ID,hap_accessory_IDBYTES,accessory.LTPK,crypto_sign_PUBLICKEYBYTES,NULL); TempBuffer<uint8_t> accessoryInfo(accessoryX,accessoryX.len(),accessory.ID,hap_accessory_IDBYTES,accessory.LTPK,crypto_sign_PUBLICKEYBYTES,NULL);
subTLV.clear(); // clear existing SUBTLV records subTLV.clear(); // clear existing SUBTLV records
subTLV.add(kTLVType_Signature,64,NULL); // create blank Signature TLV with space for 64 bytes itSignature=subTLV.add(kTLVType_Signature,64,NULL); // create blank Signature TLV with space for 64 bytes
crypto_sign_detached(subTLV.back(),NULL,accessoryInfo,accessoryInfo.len(),accessory.LTSK); // produce signature of accessoryInfo using AccessoryLTSK (Ed25519 long-term secret key)
subTLV.add(kTLVType_Identifier,hap_accessory_IDBYTES,accessory.ID); // set Identifier TLV record as accessoryPairingID crypto_sign_detached(*itSignature,NULL,accessoryInfo,accessoryInfo.len(),accessory.LTSK); // produce signature of accessoryInfo using AccessoryLTSK (Ed25519 long-term secret key)
subTLV.add(kTLVType_PublicKey,crypto_sign_PUBLICKEYBYTES,accessory.LTPK); // set PublicKey TLV record as accessoryLTPK
subTLV.add(kTLVType_Identifier,hap_accessory_IDBYTES,accessory.ID); // set Identifier TLV record as accessoryPairingID
subTLV.add(kTLVType_PublicKey,crypto_sign_PUBLICKEYBYTES,accessory.LTPK); // set PublicKey TLV record as accessoryLTPK
LOG2("------- ENCRYPTING SUB-TLVS -------\n"); LOG2("------- ENCRYPTING SUB-TLVS -------\n");
@ -530,9 +532,9 @@ int HAPClient::postPairSetupURL(uint8_t *content, size_t len){
// Encrypt the subTLV data using the same SRP Session Key as above with ChaCha20-Poly1305 // Encrypt the subTLV data using the same SRP Session Key as above with ChaCha20-Poly1305
responseTLV.add(kTLVType_EncryptedData,subPack.len()+crypto_aead_chacha20poly1305_IETF_ABYTES,NULL); //create blank EncryptedData TLV with space for subTLV + Authentication Tag itEncryptedData=responseTLV.add(kTLVType_EncryptedData,subPack.len()+crypto_aead_chacha20poly1305_IETF_ABYTES,NULL); //create blank EncryptedData TLV with space for subTLV + Authentication Tag
crypto_aead_chacha20poly1305_ietf_encrypt(responseTLV.back(),NULL,subPack,subPack.len(),NULL,0,NULL,(unsigned char *)"\x00\x00\x00\x00PS-Msg06",sessionKey); crypto_aead_chacha20poly1305_ietf_encrypt(*itEncryptedData,NULL,subPack,subPack.len(),NULL,0,NULL,(unsigned char *)"\x00\x00\x00\x00PS-Msg06",sessionKey);
LOG2("---------- END SUB-TLVS! ----------\n"); LOG2("---------- END SUB-TLVS! ----------\n");
@ -621,8 +623,8 @@ int HAPClient::postPairVerifyURL(uint8_t *content, size_t len){
TempBuffer<uint8_t> accessoryInfo(publicCurveKey,crypto_box_PUBLICKEYBYTES,accessory.ID,hap_accessory_IDBYTES,iosCurveKey,crypto_box_PUBLICKEYBYTES,NULL); TempBuffer<uint8_t> accessoryInfo(publicCurveKey,crypto_box_PUBLICKEYBYTES,accessory.ID,hap_accessory_IDBYTES,iosCurveKey,crypto_box_PUBLICKEYBYTES,NULL);
subTLV.add(kTLVType_Identifier,hap_accessory_IDBYTES,accessory.ID); // set Identifier subTLV record as Accessory's Pairing ID subTLV.add(kTLVType_Identifier,hap_accessory_IDBYTES,accessory.ID); // set Identifier subTLV record as Accessory's Pairing ID
subTLV.add(kTLVType_Signature,crypto_sign_BYTES,NULL); // create blank Signature subTLV auto itSignature=subTLV.add(kTLVType_Signature,crypto_sign_BYTES,NULL); // create blank Signature subTLV
crypto_sign_detached(subTLV.back(),NULL,accessoryInfo,accessoryInfo.len(),accessory.LTSK); // produce Signature of accessoryInfo using Accessory's LTSK crypto_sign_detached(*itSignature,NULL,accessoryInfo,accessoryInfo.len(),accessory.LTSK); // produce Signature of accessoryInfo using Accessory's LTSK
LOG2("------- ENCRYPTING SUB-TLVS -------\n"); LOG2("------- ENCRYPTING SUB-TLVS -------\n");
@ -638,8 +640,8 @@ int HAPClient::postPairVerifyURL(uint8_t *content, size_t len){
sessionKey=(uint8_t *)HS_MALLOC(crypto_box_PUBLICKEYBYTES); // temporary space - will be deleted at end of verification process sessionKey=(uint8_t *)HS_MALLOC(crypto_box_PUBLICKEYBYTES); // temporary space - will be deleted at end of verification process
HKDF::create(sessionKey,sharedCurveKey,crypto_box_PUBLICKEYBYTES,"Pair-Verify-Encrypt-Salt","Pair-Verify-Encrypt-Info"); // create Session Curve25519 Key from Shared-Secret Curve25519 Key using HKDF-SHA-512 HKDF::create(sessionKey,sharedCurveKey,crypto_box_PUBLICKEYBYTES,"Pair-Verify-Encrypt-Salt","Pair-Verify-Encrypt-Info"); // create Session Curve25519 Key from Shared-Secret Curve25519 Key using HKDF-SHA-512
responseTLV.add(kTLVType_EncryptedData,subPack.len()+crypto_aead_chacha20poly1305_IETF_ABYTES,NULL); // create blank EncryptedData subTLV auto itEncryptedData=responseTLV.add(kTLVType_EncryptedData,subPack.len()+crypto_aead_chacha20poly1305_IETF_ABYTES,NULL); // create blank EncryptedData subTLV
crypto_aead_chacha20poly1305_ietf_encrypt(responseTLV.back(),NULL,subPack,subPack.len(),NULL,0,NULL,(unsigned char *)"\x00\x00\x00\x00PV-Msg02",sessionKey); // encrypt data with Session Curve25519 Key and padded nonce="PV-Msg02" crypto_aead_chacha20poly1305_ietf_encrypt(*itEncryptedData,NULL,subPack,subPack.len(),NULL,0,NULL,(unsigned char *)"\x00\x00\x00\x00PV-Msg02",sessionKey); // encrypt data with Session Curve25519 Key and padded nonce="PV-Msg02"
LOG2("---------- END SUB-TLVS! ----------\n"); LOG2("---------- END SUB-TLVS! ----------\n");

View File

@ -68,27 +68,28 @@ void tlv8_t::osprint(std::ostream& os){
///////////////////////////////////// /////////////////////////////////////
TLV8 &TLV8::add(uint8_t tag, size_t len, const uint8_t* val){ TLV8_it TLV8::add(uint8_t tag, size_t len, const uint8_t* val){
if(!empty() && back().tag==tag) if(!empty() && back().tag==tag)
back().update(len,val); back().update(len,val);
else else
emplace_back(tag,len,val); emplace_back(tag,len,val);
return(*this); return(--end());
} }
///////////////////////////////////// /////////////////////////////////////
TLV8 &TLV8::add(uint8_t tag, TLV8 &subTLV){ TLV8_it TLV8::add(uint8_t tag, TLV8 &subTLV){
subTLV.pack(add(tag,subTLV.pack_size(),NULL).back()); // add new blank element of sufficient size and pack subTLV into this new element auto it=add(tag,subTLV.pack_size(),NULL); // create space for inserting sub TLV and store iterator to new element
return(*this); subTLV.pack(*it); // pack subTLV into new element
return(--end());
} }
///////////////////////////////////// /////////////////////////////////////
TLV8 &TLV8::add(uint8_t tag, uint64_t val){ TLV8_it TLV8::add(uint8_t tag, uint64_t val){
uint8_t *p=reinterpret_cast<uint8_t *>(&val); uint8_t *p=reinterpret_cast<uint8_t *>(&val);
size_t nBytes=sizeof(uint64_t); size_t nBytes=sizeof(uint64_t);
@ -104,7 +105,7 @@ TLV8_it TLV8::find(uint8_t tag, TLV8_it it1, TLV8_it it2){
auto it=it1; auto it=it1;
while(it!=it2 && (*it).tag!=tag) while(it!=it2 && (*it).tag!=tag)
it++; it++;
return(it); return(it==it2?end():it);
} }
///////////////////////////////////// /////////////////////////////////////

View File

@ -84,11 +84,11 @@ class TLV8 : public std::list<tlv8_t, Mallocator<tlv8_t>> {
TLV8(){}; TLV8(){};
TLV8(const TLV8_names *names, int nNames) : names{names}, nNames{nNames} {}; TLV8(const TLV8_names *names, int nNames) : names{names}, nNames{nNames} {};
TLV8 & add(uint8_t tag, size_t len, const uint8_t *val); TLV8_it add(uint8_t tag, size_t len, const uint8_t *val);
TLV8 & add(uint8_t tag, uint64_t val); TLV8_it add(uint8_t tag, uint64_t val);
TLV8 & add(uint8_t tag, TLV8 &subTLV); TLV8_it add(uint8_t tag, TLV8 &subTLV);
TLV8 & add(uint8_t tag){return(add(tag, 0, NULL));} TLV8_it add(uint8_t tag){return(add(tag, 0, NULL));}
TLV8 & add(uint8_t tag, const char *val){return(add(tag, strlen(val), reinterpret_cast<const uint8_t*>(val)));} TLV8_it add(uint8_t tag, const char *val){return(add(tag, strlen(val), reinterpret_cast<const uint8_t*>(val)));}
TLV8_it find(uint8_t tag, TLV8_it it1, TLV8_it it2); TLV8_it find(uint8_t tag, TLV8_it it1, TLV8_it it2);
TLV8_it find(uint8_t tag, TLV8_it it1){return(find(tag, it1, end()));} TLV8_it find(uint8_t tag, TLV8_it it1){return(find(tag, it1, end()));}