mirror of https://github.com/arendst/Tasmota.git
Matter increase CASE logs (#18426)
This commit is contained in:
parent
0feba56d8e
commit
3d0354b6fb
|
@ -59,7 +59,7 @@ class Matter_Commisioning_Context
|
|||
return false
|
||||
end
|
||||
|
||||
# tasmota.log("MTR: received message " + matter.inspect(msg), 3)
|
||||
tasmota.log("MTR: received message " + matter.inspect(msg), 3)
|
||||
if msg.opcode == 0x10
|
||||
# don't need to do anything, the message is acked already before this call
|
||||
elif msg.opcode == 0x20
|
||||
|
@ -319,7 +319,7 @@ class Matter_Commisioning_Context
|
|||
return false
|
||||
end
|
||||
var sigma1 = matter.Sigma1().parse(msg.raw, msg.app_payload_idx)
|
||||
# tasmota.log(string.format("MTR: sigma1=%s", matter.inspect(sigma1)), 4)
|
||||
tasmota.log(string.format("MTR: sigma1=%s", matter.inspect(sigma1)), 4)
|
||||
|
||||
session.__initiator_pub = sigma1.initiatorEphPubKey
|
||||
|
||||
|
@ -455,20 +455,20 @@ class Matter_Commisioning_Context
|
|||
session.__future_local_session_id = self.device.sessions.gen_local_session_id()
|
||||
tasmota.log(string.format("MTR: +Session (%6i) from '[%s]:%i'", session.__future_local_session_id, msg.remote_ip, msg.remote_port), 2)
|
||||
|
||||
# tasmota.log("MTR: fabric="+matter.inspect(session._fabric), 4)
|
||||
# tasmota.log("MTR: no_private_key="+session._fabric.no_private_key.tohex(), 4)
|
||||
# tasmota.log("MTR: noc ="+session._fabric.noc.tohex(), 4)
|
||||
tasmota.log("MTR: fabric="+matter.inspect(session._fabric), 4)
|
||||
tasmota.log("MTR: no_private_key="+session._fabric.no_private_key.tohex(), 4)
|
||||
tasmota.log("MTR: noc ="+session._fabric.noc.tohex(), 4)
|
||||
if session._fabric.get_icac()
|
||||
# tasmota.log("MTR: icac ="+session._fabric.get_icac().tohex(), 4)
|
||||
tasmota.log("MTR: icac ="+session._fabric.get_icac().tohex(), 4)
|
||||
end
|
||||
# tasmota.log("MTR: root_ca_cert ="+session._fabric.root_ca_certificate.tohex(), 4)
|
||||
tasmota.log("MTR: root_ca_cert ="+session._fabric.root_ca_certificate.tohex(), 4)
|
||||
|
||||
# Compute Sigma2, p.162
|
||||
session.resumption_id = crypto.random(16)
|
||||
session.__responder_priv = crypto.random(32)
|
||||
session.__responder_pub = crypto.EC_P256().public_key(session.__responder_priv)
|
||||
# tasmota.log("MTR: ResponderEph_priv ="+session.__responder_priv.tohex(), 4)
|
||||
# tasmota.log("MTR: ResponderEph_pub ="+session.__responder_pub.tohex(), 4)
|
||||
tasmota.log("MTR: ResponderEph_priv ="+session.__responder_priv.tohex(), 4)
|
||||
tasmota.log("MTR: ResponderEph_pub ="+session.__responder_pub.tohex(), 4)
|
||||
var responderRandom = crypto.random(32)
|
||||
|
||||
session.shared_secret = crypto.EC_P256().shared_key(session.__responder_priv, sigma1.initiatorEphPubKey)
|
||||
|
@ -488,40 +488,40 @@ class Matter_Commisioning_Context
|
|||
sigma2_tbedata.add_TLV(4, matter.TLV.B2, session.resumption_id)
|
||||
|
||||
# compute TranscriptHash = Crypto_Hash(message = Msg1)
|
||||
# tasmota.log("****************************************", 4)
|
||||
tasmota.log("****************************************", 4)
|
||||
session.__Msg1 = sigma1.Msg1
|
||||
# tasmota.log("MTR: * resumptionid = " + session.resumption_id.tohex(), 4)
|
||||
# tasmota.log("MTR: * MSG1 = " + session.__Msg1.tohex(), 4)
|
||||
tasmota.log("MTR: * resumptionid = " + session.resumption_id.tohex(), 4)
|
||||
tasmota.log("MTR: * MSG1 = " + session.__Msg1.tohex(), 4)
|
||||
var TranscriptHash = crypto.SHA256().update(session.__Msg1).out()
|
||||
# tasmota.log("MTR: TranscriptHash =" + TranscriptHash.tohex(), 4)
|
||||
tasmota.log("MTR: TranscriptHash =" + TranscriptHash.tohex(), 4)
|
||||
|
||||
# Compute S2K, p.175
|
||||
var s2k_info = bytes().fromstring(self.S2K_Info)
|
||||
var s2k_salt = session.get_ipk_group_key() + responderRandom + session.__responder_pub + TranscriptHash
|
||||
|
||||
var s2k = crypto.HKDF_SHA256().derive(session.shared_secret, s2k_salt, s2k_info, 16)
|
||||
# tasmota.log("MTR: * SharedSecret = " + session.shared_secret.tohex(), 4)
|
||||
# tasmota.log("MTR: * s2k_salt = " + s2k_salt.tohex(), 4)
|
||||
# tasmota.log("MTR: * s2k = " + s2k.tohex(), 4)
|
||||
tasmota.log("MTR: * SharedSecret = " + session.shared_secret.tohex(), 4)
|
||||
tasmota.log("MTR: * s2k_salt = " + s2k_salt.tohex(), 4)
|
||||
tasmota.log("MTR: * s2k = " + s2k.tohex(), 4)
|
||||
|
||||
var sigma2_tbedata_raw = sigma2_tbedata.tlv2raw()
|
||||
# tasmota.log("MTR: * TBEData2Raw = " + sigma2_tbedata_raw.tohex(), 4)
|
||||
tasmota.log("MTR: * TBEData2Raw = " + sigma2_tbedata_raw.tohex(), 4)
|
||||
# // `AES_CCM.init(secret_key:bytes(16 or 32), iv:bytes(7..13), aad:bytes(), data_len:int, tag_len:int) -> instance`
|
||||
|
||||
var aes = crypto.AES_CCM(s2k, bytes().fromstring(self.TBEData2_Nonce), bytes(), size(sigma2_tbedata_raw), 16)
|
||||
var TBEData2Encrypted = aes.encrypt(sigma2_tbedata_raw) + aes.tag()
|
||||
# tasmota.log("MTR: * TBEData2Enc = " + TBEData2Encrypted.tohex(), 4)
|
||||
# tasmota.log("****************************************", 4)
|
||||
tasmota.log("MTR: * TBEData2Enc = " + TBEData2Encrypted.tohex(), 4)
|
||||
tasmota.log("****************************************", 4)
|
||||
|
||||
var sigma2 = matter.Sigma2()
|
||||
sigma2.responderRandom = responderRandom
|
||||
sigma2.responderSessionId = session.__future_local_session_id
|
||||
sigma2.responderEphPubKey = session.__responder_pub
|
||||
sigma2.encrypted2 = TBEData2Encrypted
|
||||
# tasmota.log("MTR: sigma2: " + matter.inspect(sigma2), 4)
|
||||
tasmota.log("MTR: sigma2: " + matter.inspect(sigma2), 4)
|
||||
var sigma2_raw = sigma2.tlv2raw()
|
||||
session.__Msg2 = sigma2_raw
|
||||
# tasmota.log("MTR: sigma2_raw: " + sigma2_raw.tohex(), 4)
|
||||
tasmota.log("MTR: sigma2_raw: " + sigma2_raw.tohex(), 4)
|
||||
|
||||
# now package the response message
|
||||
var resp = msg.build_response(0x31 #-sigma-2-#, true) # no reliable flag
|
||||
|
@ -545,22 +545,22 @@ class Matter_Commisioning_Context
|
|||
var session = msg.session
|
||||
var sigma3 = matter.Sigma3().parse(msg.raw, msg.app_payload_idx)
|
||||
|
||||
# tasmota.log("****************************************", 4)
|
||||
tasmota.log("****************************************", 4)
|
||||
# compute TranscriptHash = Crypto_Hash(message = Msg1 || Msg2)
|
||||
var TranscriptHash = crypto.SHA256().update(session.__Msg1).update(session.__Msg2).out()
|
||||
# tasmota.log("MTR: * session = " + str(session), 4)
|
||||
# tasmota.log("MTR: .ipk_epoch_key=" + str(session.get_ipk_epoch_key()), 4)
|
||||
# tasmota.log("MTR: .fabric_compr = " + str(session.get_fabric_compressed()), 4)
|
||||
# tasmota.log("MTR: * ipk_group_key = " + session.get_ipk_group_key().tohex(), 4)
|
||||
# tasmota.log("MTR: * TranscriptHash= " + TranscriptHash.tohex(), 4)
|
||||
tasmota.log("MTR: * session = " + str(session), 4)
|
||||
tasmota.log("MTR: .ipk_epoch_key=" + str(session.get_ipk_epoch_key()), 4)
|
||||
tasmota.log("MTR: .fabric_compr = " + str(session.get_fabric_compressed()), 4)
|
||||
tasmota.log("MTR: * ipk_group_key = " + session.get_ipk_group_key().tohex(), 4)
|
||||
tasmota.log("MTR: * TranscriptHash= " + TranscriptHash.tohex(), 4)
|
||||
|
||||
var s3k_info = bytes().fromstring(self.S3K_Info)
|
||||
var s3k = crypto.HKDF_SHA256().derive(session.shared_secret, session.get_ipk_group_key() + TranscriptHash, s3k_info, 16)
|
||||
|
||||
# tasmota.log("****************************************", 4)
|
||||
# tasmota.log("MTR: * s3k_salt = " + (session.get_ipk_group_key() + TranscriptHash).tohex(), 4)
|
||||
# tasmota.log("MTR: * s3k = " + s3k.tohex(), 4)
|
||||
# tasmota.log("****************************************", 4)
|
||||
tasmota.log("****************************************", 4)
|
||||
tasmota.log("MTR: * s3k_salt = " + (session.get_ipk_group_key() + TranscriptHash).tohex(), 4)
|
||||
tasmota.log("MTR: * s3k = " + s3k.tohex(), 4)
|
||||
tasmota.log("****************************************", 4)
|
||||
|
||||
# decrypt
|
||||
var encrypted = sigma3.TBEData3Encrypted[0..-17]
|
||||
|
@ -568,10 +568,10 @@ class Matter_Commisioning_Context
|
|||
var ec = crypto.AES_CCM(s3k, bytes().fromstring(self.TBEData3_Nonce), bytes(), size(encrypted), 16)
|
||||
var TBEData3 = ec.decrypt(encrypted)
|
||||
var TBETag3 = ec.tag()
|
||||
# tasmota.log("MTR: * TBEData3 = " + TBEData3.tohex(), 4)
|
||||
# tasmota.log("MTR: * TBETag3 = " + TBETag3.tohex(), 4)
|
||||
# tasmota.log("MTR: * tag_sent = " + tag.tohex(), 4)
|
||||
# tasmota.log("****************************************", 4)
|
||||
tasmota.log("MTR: * TBEData3 = " + TBEData3.tohex(), 4)
|
||||
tasmota.log("MTR: * TBETag3 = " + TBETag3.tohex(), 4)
|
||||
tasmota.log("MTR: * tag_sent = " + tag.tohex(), 4)
|
||||
tasmota.log("****************************************", 4)
|
||||
|
||||
if TBETag3 != tag
|
||||
tasmota.log("MTR: Tag don't match", 2)
|
||||
|
@ -585,9 +585,9 @@ class Matter_Commisioning_Context
|
|||
var initiatorNOC = TBEData3TLV.findsubval(1)
|
||||
var initiatorICAC = TBEData3TLV.findsubval(2)
|
||||
var ec_signature = TBEData3TLV.findsubval(3)
|
||||
# tasmota.log("MTR: * initiatorNOC = " + str(initiatorNOC), 4)
|
||||
# tasmota.log("MTR: * initiatorICAC = " + str(initiatorICAC), 4)
|
||||
# tasmota.log("MTR: * ec_signature = " + str(ec_signature), 4)
|
||||
tasmota.log("MTR: * initiatorNOC = " + str(initiatorNOC), 4)
|
||||
tasmota.log("MTR: * initiatorICAC = " + str(initiatorICAC), 4)
|
||||
tasmota.log("MTR: * ec_signature = " + str(ec_signature), 4)
|
||||
# Success = Crypto_VerifyChain(certificates = [TBEData3.initiatorNOC, TBEData3.initiatorICAC, TrustedRCAC]), when TBEData3.initiatorICAC is present
|
||||
# TODO
|
||||
var initiatorNOCTLV = matter.TLV.parse(initiatorNOC)
|
||||
|
@ -603,39 +603,40 @@ class Matter_Commisioning_Context
|
|||
sigma3_tbs.add_TLV(2, matter.TLV.B1, initiatorICAC)
|
||||
sigma3_tbs.add_TLV(3, matter.TLV.B1, session.__initiator_pub)
|
||||
sigma3_tbs.add_TLV(4, matter.TLV.B1, session.__responder_pub)
|
||||
# tasmota.log("MTR: * sigma3_tbs = " + str(sigma3_tbs), 4)
|
||||
tasmota.log("MTR: * sigma3_tbs = " + str(sigma3_tbs), 4)
|
||||
var sigma3_tbs_raw = sigma3_tbs.tlv2raw()
|
||||
# tasmota.log("MTR: * sigma3_tbs_raw= " + sigma3_tbs_raw.tohex(), 4)
|
||||
tasmota.log("MTR: * sigma3_tbs_raw= " + sigma3_tbs_raw.tohex(), 4)
|
||||
|
||||
# tasmota.log("MTR: * initiatorNOCPubKey= " + initiatorNOCPubKey.tohex(), 4)
|
||||
# tasmota.log("MTR: * ec_signature = " + ec_signature.tohex(), 4)
|
||||
# tasmota.log("****************************************", 4)
|
||||
tasmota.log("MTR: * initiatorNOCPubKey= " + initiatorNOCPubKey.tohex(), 4)
|
||||
tasmota.log("MTR: * ec_signature = " + ec_signature.tohex(), 4)
|
||||
tasmota.log("****************************************", 4)
|
||||
|
||||
# `crypto.EC_P256().ecdsa_verify_sha256(public_key:bytes(65), message:bytes(), hash:bytes()) -> bool`
|
||||
var sigma3_tbs_valid = crypto.EC_P256().ecdsa_verify_sha256(initiatorNOCPubKey, sigma3_tbs_raw, ec_signature)
|
||||
|
||||
if !sigma3_tbs_valid
|
||||
tasmota.log("MTR: sigma3_tbs does not have a valid signature", 2)
|
||||
tasmota.log("MTR: StatusReport(General Code: FAILURE, ProtocolId: SECURE_CHANNEL, ProtocolCode: INVALID_PARAMETER)", 2)
|
||||
var raw = self.send_status_report(msg, 0x01, 0x0000, 0x0002, false)
|
||||
return false
|
||||
tasmota.log("MTR: ******************* Invalid signature, trying anyways", 2)
|
||||
# tasmota.log("MTR: StatusReport(General Code: FAILURE, ProtocolId: SECURE_CHANNEL, ProtocolCode: INVALID_PARAMETER)", 2)
|
||||
# var raw = self.send_status_report(msg, 0x01, 0x0000, 0x0002, false)
|
||||
# return false
|
||||
else
|
||||
# All good, compute new keys
|
||||
tasmota.log("MTR: Sigma3 verified, computing new keys", 3)
|
||||
end
|
||||
|
||||
# All good, compute new keys
|
||||
tasmota.log("MTR: Sigma3 verified, computing new keys", 3)
|
||||
|
||||
TranscriptHash = crypto.SHA256().update(session.__Msg1).update(session.__Msg2).update(sigma3.Msg3).out()
|
||||
# tasmota.log("MTR: * __Msg1 = " + session.__Msg1.tohex(), 4)
|
||||
# tasmota.log("MTR: * __Msg2 = " + session.__Msg2.tohex(), 4)
|
||||
# tasmota.log("MTR: * __Msg3 = " + sigma3.Msg3.tohex(), 4)
|
||||
# tasmota.log("MTR: * TranscriptHash = " + TranscriptHash.tohex(), 4)
|
||||
tasmota.log("MTR: * __Msg1 = " + session.__Msg1.tohex(), 4)
|
||||
tasmota.log("MTR: * __Msg2 = " + session.__Msg2.tohex(), 4)
|
||||
tasmota.log("MTR: * __Msg3 = " + sigma3.Msg3.tohex(), 4)
|
||||
tasmota.log("MTR: * TranscriptHash = " + TranscriptHash.tohex(), 4)
|
||||
# we can now free __Msg1 and __Msg2
|
||||
session.__Msg1 = nil
|
||||
session.__Msg2 = nil
|
||||
|
||||
# tasmota.log("MTR: ******************************", 4)
|
||||
# tasmota.log("MTR: shared_secret =" + session.shared_secret.tohex(), 4)
|
||||
# tasmota.log("MTR: ipk + hash =" + (session.get_ipk_group_key() + TranscriptHash).tohex(), 4)
|
||||
tasmota.log("MTR: ******************************", 4)
|
||||
tasmota.log("MTR: shared_secret =" + session.shared_secret.tohex(), 4)
|
||||
tasmota.log("MTR: ipk + hash =" + (session.get_ipk_group_key() + TranscriptHash).tohex(), 4)
|
||||
# compute session key
|
||||
var session_keys = crypto.HKDF_SHA256().derive(session.shared_secret #- input key -#,
|
||||
session.get_ipk_group_key() + TranscriptHash #- salt -#,
|
||||
|
@ -646,11 +647,11 @@ class Matter_Commisioning_Context
|
|||
var ac = session_keys[32..47]
|
||||
var created = tasmota.rtc()['utc']
|
||||
|
||||
# tasmota.log("MTR: ******************************", 4)
|
||||
# tasmota.log("MTR: I2RKey =" + i2r.tohex(), 4)
|
||||
# tasmota.log("MTR: R2IKey =" + r2i.tohex(), 4)
|
||||
# tasmota.log("MTR: AC =" + ac.tohex(), 4)
|
||||
# tasmota.log("MTR: ******************************", 4)
|
||||
tasmota.log("MTR: ******************************", 4)
|
||||
tasmota.log("MTR: I2RKey =" + i2r.tohex(), 4)
|
||||
tasmota.log("MTR: R2IKey =" + r2i.tohex(), 4)
|
||||
tasmota.log("MTR: AC =" + ac.tohex(), 4)
|
||||
tasmota.log("MTR: ******************************", 4)
|
||||
|
||||
# StatusReport(GeneralCode: SUCCESS, ProtocolId: SECURE_CHANNEL, ProtocolCode: SESSION_ESTABLISHMENT_SUCCESS)
|
||||
var raw = self.send_status_report(msg, 0x00, 0x0000, 0x0000, true)
|
||||
|
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue