From e8875de69cde1d6088165f919156376fa5cb6e3c Mon Sep 17 00:00:00 2001
From: jbhusson <jonah@elixxir.io>
Date: Thu, 6 Jan 2022 12:54:07 -0500
Subject: [PATCH] Fix garbled message bug

---
 network/message/garbled.go | 111 ++++++++++++++++++++-----------------
 network/message/handler.go |   5 +-
 2 files changed, 63 insertions(+), 53 deletions(-)

diff --git a/network/message/garbled.go b/network/message/garbled.go
index 9388f4dcf..537fedc04 100644
--- a/network/message/garbled.go
+++ b/network/message/garbled.go
@@ -57,65 +57,72 @@ func (m *Manager) handleGarbledMessages() {
 	var failedMsgs []format.Message
 	//try to decrypt every garbled message, excising those who's counts are too high
 	for grbldMsg, count, timestamp, has := garbledMsgs.Next(); has; grbldMsg, count, timestamp, has = garbledMsgs.Next() {
-		fingerprint := grbldMsg.GetKeyFP()
-		// Check if the key is there, process it if it is
-		if key, isE2E := e2eKv.PopKey(fingerprint); isE2E {
-			jww.INFO.Printf("[GARBLE] Check E2E for %s, KEYFP: %s",
-				grbldMsg.Digest(), grbldMsg.GetKeyFP())
-			// Decrypt encrypted message
-			msg, err := key.Decrypt(grbldMsg)
-			if err == nil {
-				// get the sender
-				sender := key.GetSession().GetPartner()
-				//remove from the buffer if decryption is successful
-				garbledMsgs.Remove(grbldMsg)
+		//if it exists, check against all in the list
+		modifiedContents := append([]byte{0}, grbldMsg.GetContents()...)
+		identity := m.Session.GetUser().GetContact().ID
+		_, forMe, _ := m.Session.GetEdge().Check(identity, grbldMsg.GetIdentityFP(), modifiedContents)
+		if forMe {
+			fingerprint := grbldMsg.GetKeyFP()
+			// Check if the key is there, process it if it is
+			if key, isE2E := e2eKv.PopKey(fingerprint); isE2E {
+				jww.INFO.Printf("[GARBLE] Check E2E for %s, KEYFP: %s",
+					grbldMsg.Digest(), grbldMsg.GetKeyFP())
+				// Decrypt encrypted message
+				msg, err := key.Decrypt(grbldMsg)
+				if err == nil {
+					// get the sender
+					sender := key.GetSession().GetPartner()
+					//remove from the buffer if decryption is successful
+					garbledMsgs.Remove(grbldMsg)
 
-				jww.INFO.Printf("[GARBLE] message decoded as E2E from "+
-					"%s, msgDigest: %s", sender, grbldMsg.Digest())
+					jww.INFO.Printf("[GARBLE] message decoded as E2E from "+
+						"%s, msgDigest: %s", sender, grbldMsg.Digest())
 
-				//handle the successfully decrypted message
-				xxMsg, ok := m.partitioner.HandlePartition(sender, message.E2E,
-					msg.GetContents(),
-					key.GetSession().GetRelationshipFingerprint())
-				if ok {
-					m.Switchboard.Speak(xxMsg)
-					continue
-				}
-			}
-		} else {
-			// todo: figure out how to get the ephermal reception id in here.
-			// we have the raw data, but do not know what address space was
-			// used int he round
-			// todo: figure out how to get the round id, the recipient id, and the round timestamp
-			/*
-				ephid, err := ephemeral.Marshal(garbledMsg.GetEphemeralRID())
-				if err!=nil{
-					jww.WARN.Printf("failed to get the ephemeral id for a garbled " +
-						"message, clearing the message: %+v", err)
-					garbledMsgs.Remove(garbledMsg)
-					continue
+					//handle the successfully decrypted message
+					xxMsg, ok := m.partitioner.HandlePartition(sender, message.E2E,
+						msg.GetContents(),
+						key.GetSession().GetRelationshipFingerprint())
+					if ok {
+						m.Switchboard.Speak(xxMsg)
+						continue
+					}
 				}
+			} else {
+				// todo: figure out how to get the ephermal reception id in here.
+				// we have the raw data, but do not know what address space was
+				// used int he round
+				// todo: figure out how to get the round id, the recipient id, and the round timestamp
+				/*
+					ephid, err := ephemeral.Marshal(garbledMsg.GetEphemeralRID())
+					if err!=nil{
+						jww.WARN.Printf("failed to get the ephemeral id for a garbled " +
+							"message, clearing the message: %+v", err)
+						garbledMsgs.Remove(garbledMsg)
+						continue
+					}
 
-				ephid.Clear(m.)*/
+					ephid.Clear(m.)*/
 
-			raw := message.Receive{
-				Payload:        grbldMsg.Marshal(),
-				MessageType:    message.Raw,
-				Sender:         &id.ID{},
-				EphemeralID:    ephemeral.Id{},
-				Timestamp:      time.Time{},
-				Encryption:     message.None,
-				RecipientID:    &id.ID{},
-				RoundId:        0,
-				RoundTimestamp: time.Time{},
+				raw := message.Receive{
+					Payload:        grbldMsg.Marshal(),
+					MessageType:    message.Raw,
+					Sender:         &id.ID{},
+					EphemeralID:    ephemeral.Id{},
+					Timestamp:      time.Time{},
+					Encryption:     message.None,
+					RecipientID:    &id.ID{},
+					RoundId:        0,
+					RoundTimestamp: time.Time{},
+				}
+				im := fmt.Sprintf("[GARBLE] RAW Message reprecessed: keyFP: %v, "+
+					"msgDigest: %s", grbldMsg.GetKeyFP(), grbldMsg.Digest())
+				jww.INFO.Print(im)
+				m.Internal.Events.Report(1, "MessageReception", "Garbled", im)
+				m.Session.GetGarbledMessages().Add(grbldMsg)
+				m.Switchboard.Speak(raw)
 			}
-			im := fmt.Sprintf("[GARBLE] RAW Message reprecessed: keyFP: %v, "+
-				"msgDigest: %s", grbldMsg.GetKeyFP(), grbldMsg.Digest())
-			jww.INFO.Print(im)
-			m.Internal.Events.Report(1, "MessageReception", "Garbled", im)
-			m.Session.GetGarbledMessages().Add(grbldMsg)
-			m.Switchboard.Speak(raw)
 		}
+
 		// fail the message if any part of the decryption fails,
 		// unless it is the last attempts and has been in the buffer long
 		// enough, in which case remove it
diff --git a/network/message/handler.go b/network/message/handler.go
index 093287d4e..abf4e5da2 100644
--- a/network/message/handler.go
+++ b/network/message/handler.go
@@ -71,7 +71,10 @@ func (m *Manager) handleMessage(ecrMsg format.Message, bundle Bundle, edge *edge
 				"check: %v (expected-default) vs %v (received)", identity.EphId,
 				identity.Source, expectedFP, ecrMsg.GetIdentityFP())
 		}
-
+		im := fmt.Sprintf("Garbled/RAW Message: keyFP: %v, round: %d"+
+			"msgDigest: %s, not determined to be for client", msg.GetKeyFP(), bundle.Round, msg.Digest())
+		m.Internal.Events.Report(1, "MessageReception", "Garbled", im)
+		m.Session.GetGarbledMessages().Add(msg)
 		return
 	}
 
-- 
GitLab