@@ -106,6 +106,7 @@ func NewHandshakeParams() *HandshakeParams {
106106type HandshakeController struct {
107107 pss * Pss
108108 keyC map [string ]chan []string // adds a channel to report when a handshake succeeds
109+ keyCMu sync.Mutex // protects keyC map
109110 lock sync.Mutex
110111 symKeyRequestTimeout time.Duration
111112 symKeyExpiryTimeout time.Duration
@@ -165,9 +166,9 @@ func (ctl *HandshakeController) validKeys(pubkeyid string, topic *Topic, in bool
165166
166167 for _ , key := range * keystore {
167168 if key .limit <= key .count {
168- ctl .releaseKey (* key .symKeyID , topic )
169+ ctl .releaseKeyNoLock (* key .symKeyID , topic )
169170 } else if ! key .expiredAt .IsZero () && key .expiredAt .Before (now ) {
170- ctl .releaseKey (* key .symKeyID , topic )
171+ ctl .releaseKeyNoLock (* key .symKeyID , topic )
171172 } else {
172173 validkeys = append (validkeys , key .symKeyID )
173174 }
@@ -205,15 +206,23 @@ func (ctl *HandshakeController) updateKeys(pubkeyid string, topic *Topic, in boo
205206 limit : limit ,
206207 }
207208 * keystore = append (* keystore , storekey )
209+ ctl .pss .mx .Lock ()
208210 ctl .pss .symKeyPool [* storekey .symKeyID ][* topic ].protected = true
211+ ctl .pss .mx .Unlock ()
209212 }
210213 for i := 0 ; i < len (* keystore ); i ++ {
211214 ctl .symKeyIndex [* (* keystore )[i ].symKeyID ] = & ((* keystore )[i ])
212215 }
213216}
214217
215- // Expire a symmetric key, making it elegible for garbage collection
216218func (ctl * HandshakeController ) releaseKey (symkeyid string , topic * Topic ) bool {
219+ ctl .lock .Lock ()
220+ defer ctl .lock .Unlock ()
221+ return ctl .releaseKeyNoLock (symkeyid , topic )
222+ }
223+
224+ // Expire a symmetric key, making it eligible for garbage collection
225+ func (ctl * HandshakeController ) releaseKeyNoLock (symkeyid string , topic * Topic ) bool {
217226 if ctl .symKeyIndex [symkeyid ] == nil {
218227 log .Debug ("no symkey" , "symkeyid" , symkeyid )
219228 return false
@@ -276,30 +285,49 @@ func (ctl *HandshakeController) clean() {
276285 }
277286}
278287
288+ func (ctl * HandshakeController ) getSymKey (symkeyid string ) * handshakeKey {
289+ ctl .lock .Lock ()
290+ defer ctl .lock .Unlock ()
291+ return ctl .symKeyIndex [symkeyid ]
292+ }
293+
279294// Passed as a PssMsg handler for the topic handshake is activated on
280295// Handles incoming key exchange messages and
281- // ccunts message usage by symmetric key (expiry limit control)
296+ // counts message usage by symmetric key (expiry limit control)
282297// Only returns error if key handler fails
283298func (ctl * HandshakeController ) handler (msg []byte , p * p2p.Peer , asymmetric bool , symkeyid string ) error {
284- if ! asymmetric {
285- if ctl .symKeyIndex [symkeyid ] != nil {
286- if ctl .symKeyIndex [symkeyid ].count >= ctl .symKeyIndex [symkeyid ].limit {
287- return fmt .Errorf ("discarding message using expired key: %s" , symkeyid )
299+ if asymmetric {
300+ keymsg := & handshakeMsg {}
301+ err := rlp .DecodeBytes (msg , keymsg )
302+ if err == nil {
303+ err := ctl .handleKeys (symkeyid , keymsg )
304+ if err != nil {
305+ log .Error ("handlekeys fail" , "error" , err )
288306 }
289- ctl .symKeyIndex [symkeyid ].count ++
290- log .Trace ("increment symkey recv use" , "symsymkeyid" , symkeyid , "count" , ctl .symKeyIndex [symkeyid ].count , "limit" , ctl .symKeyIndex [symkeyid ].limit , "receiver" , common .ToHex (crypto .FromECDSAPub (ctl .pss .PublicKey ())))
307+ return err
291308 }
292309 return nil
293310 }
294- keymsg := & handshakeMsg {}
295- err := rlp .DecodeBytes (msg , keymsg )
296- if err == nil {
297- err := ctl .handleKeys (symkeyid , keymsg )
298- if err != nil {
299- log .Error ("handlekeys fail" , "error" , err )
300- }
301- return err
311+ return ctl .registerSymKeyUse (symkeyid )
312+ }
313+
314+ func (ctl * HandshakeController ) registerSymKeyUse (symkeyid string ) error {
315+ ctl .lock .Lock ()
316+ defer ctl .lock .Unlock ()
317+
318+ symKey , ok := ctl .symKeyIndex [symkeyid ]
319+ if ! ok {
320+ return nil
302321 }
322+
323+ if symKey .count >= symKey .limit {
324+ return fmt .Errorf ("symetric key expired (id: %s)" , symkeyid )
325+ }
326+ symKey .count ++
327+
328+ receiver := common .ToHex (crypto .FromECDSAPub (ctl .pss .PublicKey ()))
329+ log .Trace ("increment symkey recv use" , "symsymkeyid" , symkeyid , "count" , symKey .count , "limit" , symKey .limit , "receiver" , receiver )
330+
303331 return nil
304332}
305333
@@ -417,6 +445,8 @@ func (ctl *HandshakeController) sendKey(pubkeyid string, topic *Topic, keycount
417445
418446// Enables callback for keys received from a key exchange request
419447func (ctl * HandshakeController ) alertHandshake (pubkeyid string , symkeys []string ) chan []string {
448+ ctl .keyCMu .Lock ()
449+ defer ctl .keyCMu .Unlock ()
420450 if len (symkeys ) > 0 {
421451 if _ , ok := ctl .keyC [pubkeyid ]; ok {
422452 ctl .keyC [pubkeyid ] <- symkeys
@@ -519,7 +549,7 @@ func (api *HandshakeAPI) GetHandshakeKeys(pubkeyid string, topic Topic, in bool,
519549// Returns the amount of messages the specified symmetric key
520550// is still valid for under the handshake scheme
521551func (api * HandshakeAPI ) GetHandshakeKeyCapacity (symkeyid string ) (uint16 , error ) {
522- storekey := api .ctrl .symKeyIndex [ symkeyid ]
552+ storekey := api .ctrl .getSymKey ( symkeyid )
523553 if storekey == nil {
524554 return 0 , fmt .Errorf ("invalid symkey id %s" , symkeyid )
525555 }
@@ -529,7 +559,7 @@ func (api *HandshakeAPI) GetHandshakeKeyCapacity(symkeyid string) (uint16, error
529559// Returns the byte representation of the public key in ascii hex
530560// associated with the given symmetric key
531561func (api * HandshakeAPI ) GetHandshakePublicKey (symkeyid string ) (string , error ) {
532- storekey := api .ctrl .symKeyIndex [ symkeyid ]
562+ storekey := api .ctrl .getSymKey ( symkeyid )
533563 if storekey == nil {
534564 return "" , fmt .Errorf ("invalid symkey id %s" , symkeyid )
535565 }
@@ -555,12 +585,8 @@ func (api *HandshakeAPI) ReleaseHandshakeKey(pubkeyid string, topic Topic, symke
555585// for message expiry control
556586func (api * HandshakeAPI ) SendSym (symkeyid string , topic Topic , msg hexutil.Bytes ) (err error ) {
557587 err = api .ctrl .pss .SendSym (symkeyid , topic , msg [:])
558- if api .ctrl .symKeyIndex [symkeyid ] != nil {
559- if api .ctrl .symKeyIndex [symkeyid ].count >= api .ctrl .symKeyIndex [symkeyid ].limit {
560- return errors .New ("attempted send with expired key" )
561- }
562- api .ctrl .symKeyIndex [symkeyid ].count ++
563- log .Trace ("increment symkey send use" , "symkeyid" , symkeyid , "count" , api .ctrl .symKeyIndex [symkeyid ].count , "limit" , api .ctrl .symKeyIndex [symkeyid ].limit , "receiver" , common .ToHex (crypto .FromECDSAPub (api .ctrl .pss .PublicKey ())))
588+ if otherErr := api .ctrl .registerSymKeyUse (symkeyid ); otherErr != nil {
589+ return otherErr
564590 }
565591 return err
566592}
0 commit comments