From f8ce1319580aeb929660488fe578bf41c786f1d2 Mon Sep 17 00:00:00 2001
From: "Richard T. Carback III" <rick.carback@gmail.com>
Date: Fri, 22 Apr 2022 19:48:13 +0000
Subject: [PATCH] Get auth to confirmation. This still fails the ownership
 proof

---
 auth/confirm.go            |  2 +-
 auth/receivedRequest.go    |  4 +++-
 auth/request.go            | 38 +++++++++++---------------------------
 auth/sentRequestHandler.go | 14 ++++++++++----
 auth/store/store.go        | 26 +++++++++++++++-----------
 5 files changed, 40 insertions(+), 44 deletions(-)

diff --git a/auth/confirm.go b/auth/confirm.go
index fc6c5b69c..bdefe18f0 100644
--- a/auth/confirm.go
+++ b/auth/confirm.go
@@ -147,7 +147,7 @@ func (s *state) confirm(partner contact.Contact, serviceTag string) (
 		sentRound, err = sendAuthConfirm(s.net, partner.ID, fp,
 			baseFmt.Marshal(), mac, s.event, serviceTag)
 
-		return nil
+		return err
 	})
 	return sentRound, err
 }
diff --git a/auth/receivedRequest.go b/auth/receivedRequest.go
index 50e8d261c..4bd1ed121 100644
--- a/auth/receivedRequest.go
+++ b/auth/receivedRequest.go
@@ -54,9 +54,11 @@ func (rrs *receivedRequestService) Process(message format.Message,
 		return
 	}
 
+	jww.INFO.Printf("partnerPubKeyBytes: %v", partnerPubKey.Bytes())
+
 	jww.TRACE.Printf("processing requests: \n\t MYPUBKEY: %s "+
 		"\n\t PARTNERPUBKEY: %s \n\t ECRPAYLOAD: %s \n\t MAC: %s",
-		state.e2e.GetHistoricalDHPubkey().Text(64),
+		state.e2e.GetHistoricalDHPubkey().Text(16),
 		partnerPubKey.TextVerbose(16, 0),
 		base64.StdEncoding.EncodeToString(baseFmt.data),
 		base64.StdEncoding.EncodeToString(message.GetMac()))
diff --git a/auth/request.go b/auth/request.go
index 69058971d..8a064c1fe 100644
--- a/auth/request.go
+++ b/auth/request.go
@@ -54,7 +54,8 @@ func (s *state) Request(partner contact.Contact, myfacts fact.FactList) (id.Roun
 }
 
 // request internal helper
-func (s *state) request(partner contact.Contact, myfacts fact.FactList, reset bool) (id.Round, error) {
+func (s *state) request(partner contact.Contact, myfacts fact.FactList,
+	reset bool) (id.Round, error) {
 
 	jww.INFO.Printf("request(...) called")
 
@@ -72,17 +73,18 @@ func (s *state) request(partner contact.Contact, myfacts fact.FactList, reset bo
 		partner.DhPubKey, s.e2e.GetGroup())
 	confirmFp := cAuth.MakeOwnershipProofFP(ownership)
 
-	// Add the sent request and use the return to build the send. This will
-	// replace the send with an old one if one was in process, wasting the key
-	// generation above. This is considered a reasonable loss due to the increase
-	// in code simplicity of this approach
+	// Add the sent request and use the return to build the
+	// send. This will replace the send with an old one if one was
+	// in process, wasting the key generation above. This is
+	// considered a reasonable loss due to the increase in code
+	// simplicity of this approach
 	sr, err := s.store.AddSent(partner.ID, partner.DhPubKey, dhPriv, dhPub,
 		sidhPriv, sidhPub, confirmFp, reset)
 	if err != nil {
 		if sr == nil {
 			return 0, err
 		} else {
-			jww.INFO.Printf("Resending request to %s from %s because "+
+			jww.INFO.Printf("Resending request to %s from %s as "+
 				"one was already sent", partner.ID, me)
 		}
 	}
@@ -96,7 +98,7 @@ func (s *state) request(partner contact.Contact, myfacts fact.FactList, reset bo
 	msgPayload := []byte(myfacts.Stringify() + terminator)
 
 	// Create the request packet.
-	request, mac, err := createRequestAuth(partner.ID, msgPayload, ownership,
+	request, mac, err := createRequestAuth(me, msgPayload, ownership,
 		dhPriv, dhPub, partner.DhPubKey, sidhPub,
 		s.e2e.GetGroup(), s.net.GetMaxMessageLength())
 	if err != nil {
@@ -107,26 +109,8 @@ func (s *state) request(partner contact.Contact, myfacts fact.FactList, reset bo
 	jww.TRACE.Printf("Request ECRPAYLOAD: %v", request.GetEcrPayload())
 	jww.TRACE.Printf("Request MAC: %v", mac)
 
-	jww.INFO.Printf("Requesting Auth with %s, msgDigest: %s",
-		partner.ID, format.DigestContents(contents))
-
-	//register the confirm fingerprint to pick up confirm
-	err = s.net.AddFingerprint(me, confirmFp, &receivedConfirmService{
-		s:           s,
-		SentRequest: sr,
-	})
-	if err != nil {
-		return 0, errors.Errorf("cannot register fingerprint request "+
-			"to %s from %s, bailing request: %+v", partner.ID, me,
-			err)
-	}
-
-	//register service for notification on confirmation
-	s.net.AddService(me, message.Service{
-		Identifier: confirmFp[:],
-		Tag:        s.params.getConfirmTag(reset),
-		Metadata:   partner.ID[:],
-	}, nil)
+	jww.INFO.Printf("Requesting Auth with %s, msgDigest: %s, confirmFp: %s",
+		partner.ID, format.DigestContents(contents), confirmFp)
 
 	p := cmix.GetDefaultCMIXParams()
 	p.DebugTag = "auth.Request"
diff --git a/auth/sentRequestHandler.go b/auth/sentRequestHandler.go
index 51d5b7dc9..5d336b90a 100644
--- a/auth/sentRequestHandler.go
+++ b/auth/sentRequestHandler.go
@@ -15,22 +15,27 @@ type sentRequestHandler struct {
 // Add Adds the service and fingerprints to cmix for the given sent request
 func (srh *sentRequestHandler) Add(sr *store.SentRequest) {
 	fp := sr.GetFingerprint()
+	partner := sr.GetPartner()
 	rc := &receivedConfirmService{
 		s:           srh.s,
 		SentRequest: sr,
 		notificationsService: message.Service{
 			Identifier: fp[:],
 			Tag:        srh.s.params.getConfirmTag(sr.IsReset()),
-			Metadata:   nil,
+			Metadata:   partner[:],
 		},
 	}
 
 	//add the notifications service
 	srh.s.net.AddService(srh.s.e2e.GetReceptionID(), rc.notificationsService, nil)
 
+	srFp := sr.GetFingerprint()
+	receptionID := srh.s.e2e.GetReceptionID()
+	jww.INFO.Printf("Adding SentRequest FP: %s, receptionID: %s",
+		srFp, receptionID)
+
 	//add the fingerprint
-	if err := srh.s.net.AddFingerprint(srh.s.e2e.GetReceptionID(),
-		sr.GetFingerprint(), rc); err != nil {
+	if err := srh.s.net.AddFingerprint(receptionID, srFp, rc); err != nil {
 		jww.FATAL.Panicf("failed to add a fingerprint for a auth confirm, " +
 			"this should never happen under the birthday paradox assumption of " +
 			"255 bits (the size fo the fingerprint).")
@@ -42,11 +47,12 @@ func (srh *sentRequestHandler) Add(sr *store.SentRequest) {
 // request
 func (srh *sentRequestHandler) Delete(sr *store.SentRequest) {
 	fp := sr.GetFingerprint()
+	partner := sr.GetPartner()
 
 	notificationsService := message.Service{
 		Identifier: fp[:],
 		Tag:        srh.s.params.getConfirmTag(sr.IsReset()),
-		Metadata:   nil,
+		Metadata:   partner[:],
 	}
 
 	//delete the notifications service
diff --git a/auth/store/store.go b/auth/store/store.go
index 4eaf96d4d..d9b06ae8e 100644
--- a/auth/store/store.go
+++ b/auth/store/store.go
@@ -9,6 +9,8 @@ package store
 
 import (
 	"encoding/json"
+	"sync"
+
 	"github.com/cloudflare/circl/dh/sidh"
 	"github.com/pkg/errors"
 	jww "github.com/spf13/jwalterweatherman"
@@ -19,7 +21,6 @@ import (
 	"gitlab.com/elixxir/primitives/format"
 	"gitlab.com/xx_network/primitives/id"
 	"gitlab.com/xx_network/primitives/netTime"
-	"sync"
 )
 
 const NoRequest = "Request Not Found"
@@ -173,22 +174,25 @@ func newStore(kv *versioned.KV, grp *cyclic.Group, srh SentRequestHandler) (
 }
 
 func (s *Store) AddSent(partner *id.ID, partnerHistoricalPubKey, myPrivKey,
-	myPubKey *cyclic.Int, sidHPrivA *sidh.PrivateKey, sidHPubA *sidh.PublicKey,
-	fp format.Fingerprint, reset bool) (*SentRequest, error) {
+	myPubKey *cyclic.Int, sidHPrivA *sidh.PrivateKey,
+	sidHPubA *sidh.PublicKey, fp format.Fingerprint,
+	reset bool) (*SentRequest, error) {
 	s.mux.Lock()
 	defer s.mux.Unlock()
 
 	if sentRq, ok := s.sentByID[*partner]; ok {
-		return sentRq, errors.Errorf("Cannot make new sentRequest for partner "+
-			"%s, a sent request already exists", partner)
+		return sentRq, errors.Errorf("Cannot make new sentRequest "+
+			"for partner %s, a sent request already exists",
+			partner)
 	}
 	if _, ok := s.receivedByID[*partner]; ok {
-		return nil, errors.Errorf("Cannot make new sentRequest for partner "+
-			"%s, a received reqyest already exists", partner)
+		return nil, errors.Errorf("Cannot make new sentRequest for "+
+			" partner %s, a received reqyest already exists",
+			partner)
 	}
 
-	sr, err := newSentRequest(s.kv, partner, partnerHistoricalPubKey, myPrivKey,
-		myPubKey, sidHPrivA, sidHPubA, fp, reset)
+	sr, err := newSentRequest(s.kv, partner, partnerHistoricalPubKey,
+		myPrivKey, myPubKey, sidHPrivA, sidHPubA, fp, reset)
 
 	if err != nil {
 		return nil, err
@@ -197,8 +201,8 @@ func (s *Store) AddSent(partner *id.ID, partnerHistoricalPubKey, myPrivKey,
 	s.sentByID[*sr.GetPartner()] = sr
 	s.srh.Add(sr)
 	if err = s.save(); err != nil {
-		jww.FATAL.Panicf("Failed to save Sent Request Map after adding "+
-			"partner %s", partner)
+		jww.FATAL.Panicf("Failed to save Sent Request Map after "+
+			"adding partner %s", partner)
 	}
 
 	return sr, nil
-- 
GitLab