@@ -32,17 +32,19 @@ type ECDHSession interface {
3232 GetReadyPeersCount () int
3333 ErrChan () <- chan error
3434 Close () error
35+ OnKeyExchangeComplete (callback func ())
3536}
3637
3738type ecdhSession struct {
38- nodeID string
39- peerIDs []string
40- pubSub messaging.PubSub
41- ecdhSub messaging.Subscription
42- identityStore identity.Store
43- privateKey * ecdh.PrivateKey
44- publicKey * ecdh.PublicKey
45- errCh chan error
39+ nodeID string
40+ peerIDs []string
41+ pubSub messaging.PubSub
42+ ecdhSub messaging.Subscription
43+ identityStore identity.Store
44+ privateKey * ecdh.PrivateKey
45+ publicKey * ecdh.PublicKey
46+ errCh chan error
47+ onKeyExchangeComplete func ()
4648}
4749
4850func NewECDHSession (
@@ -51,6 +53,7 @@ func NewECDHSession(
5153 pubSub messaging.PubSub ,
5254 identityStore identity.Store ,
5355) * ecdhSession {
56+ logger .Info ("Creating ECDH session" , "nodeID" , nodeID , "peerIDs" , peerIDs , "expectedKeys" , len (peerIDs ))
5457 return & ecdhSession {
5558 nodeID : nodeID ,
5659 peerIDs : peerIDs ,
@@ -72,6 +75,10 @@ func (e *ecdhSession) ErrChan() <-chan error {
7275 return e .errCh
7376}
7477
78+ func (e * ecdhSession ) OnKeyExchangeComplete (callback func ()) {
79+ e .onKeyExchangeComplete = callback
80+ }
81+
7582func (e * ecdhSession ) ListenKeyExchange () error {
7683 // Generate an ephemeral ECDH key pair
7784 privateKey , err := ecdh .X25519 ().GenerateKey (rand .Reader )
@@ -86,15 +93,19 @@ func (e *ecdhSession) ListenKeyExchange() error {
8693 sub , err := e .pubSub .Subscribe (ECDHExchangeTopic , func (natMsg * nats.Msg ) {
8794 var ecdhMsg types.ECDHMessage
8895 if err := json .Unmarshal (natMsg .Data , & ecdhMsg ); err != nil {
96+ logger .Error ("Failed to unmarshal ECDH message" , err )
8997 return
9098 }
9199
92100 if ecdhMsg .From == e .nodeID {
93101 return
94102 }
95103
104+ logger .Debug ("Received ECDH message" , "from" , ecdhMsg .From , "to" , e .nodeID )
105+
96106 //TODO: consider how to avoid replay attack
97107 if err := e .identityStore .VerifySignature (& ecdhMsg ); err != nil {
108+ logger .Error ("ECDH signature verification failed" , err , "from" , ecdhMsg .From )
98109 e .errCh <- err
99110 return
100111 }
@@ -113,7 +124,15 @@ func (e *ecdhSession) ListenKeyExchange() error {
113124 // Derive symmetric key using HKDF
114125 symmetricKey := e .deriveSymmetricKey (sharedSecret , ecdhMsg .From )
115126 e .identityStore .SetSymmetricKey (ecdhMsg .From , symmetricKey )
116- logger .Debug ("ECDH progress" , "peer" , ecdhMsg .From , "current" , e .identityStore .GetSymetricKeyCount ())
127+
128+ currentKeyCount := e .identityStore .GetSymetricKeyCount ()
129+ logger .Debug ("ECDH progress" , "peer" , ecdhMsg .From , "current" , currentKeyCount , "expected" , len (e .peerIDs ))
130+
131+ // Check if ECDH exchange is complete and notify callback
132+ if currentKeyCount == len (e .peerIDs ) && e .onKeyExchangeComplete != nil {
133+ logger .Info ("ECDH key exchange completed successfully" , "totalKeys" , currentKeyCount )
134+ e .onKeyExchangeComplete ()
135+ }
117136 })
118137
119138 e .ecdhSub = sub
0 commit comments