@@ -81,7 +81,7 @@ type senderPeer interface {
81
81
// member `protected` prevents garbage collection of the instance
82
82
type pssPeer struct {
83
83
lastSeen time.Time
84
- address * PssAddress
84
+ address PssAddress
85
85
protected bool
86
86
}
87
87
@@ -396,9 +396,11 @@ func (p *Pss) handlePssMsg(ctx context.Context, msg interface{}) error {
396
396
// raw is simplest handler contingency to check, so check that first
397
397
var isRaw bool
398
398
if pssmsg .isRaw () {
399
- if ! p .topicHandlerCaps [psstopic ].raw {
400
- log .Debug ("No handler for raw message" , "topic" , psstopic )
401
- return nil
399
+ if _ , ok := p .topicHandlerCaps [psstopic ]; ok {
400
+ if ! p .topicHandlerCaps [psstopic ].raw {
401
+ log .Debug ("No handler for raw message" , "topic" , psstopic )
402
+ return nil
403
+ }
402
404
}
403
405
isRaw = true
404
406
}
@@ -437,10 +439,10 @@ func (p *Pss) process(pssmsg *PssMsg, raw bool, prox bool) error {
437
439
var err error
438
440
var recvmsg * whisper.ReceivedMessage
439
441
var payload []byte
440
- var from * PssAddress
442
+ var from PssAddress
441
443
var asymmetric bool
442
444
var keyid string
443
- var keyFunc func (envelope * whisper.Envelope ) (* whisper.ReceivedMessage , string , * PssAddress , error )
445
+ var keyFunc func (envelope * whisper.Envelope ) (* whisper.ReceivedMessage , string , PssAddress , error )
444
446
445
447
envelope := pssmsg .Payload
446
448
psstopic := Topic (envelope .Topic )
@@ -473,7 +475,7 @@ func (p *Pss) process(pssmsg *PssMsg, raw bool, prox bool) error {
473
475
474
476
}
475
477
476
- func (p * Pss ) executeHandlers (topic Topic , payload []byte , from * PssAddress , raw bool , prox bool , asymmetric bool , keyid string ) {
478
+ func (p * Pss ) executeHandlers (topic Topic , payload []byte , from PssAddress , raw bool , prox bool , asymmetric bool , keyid string ) {
477
479
handlers := p .getHandlers (topic )
478
480
peer := p2p .NewPeer (enode.ID {}, fmt .Sprintf ("%x" , from ), []p2p.Cap {})
479
481
for h := range handlers {
@@ -528,7 +530,10 @@ func (p *Pss) isSelfPossibleRecipient(msg *PssMsg, prox bool) bool {
528
530
//
529
531
// The value in `address` will be used as a routing hint for the
530
532
// public key / topic association
531
- func (p * Pss ) SetPeerPublicKey (pubkey * ecdsa.PublicKey , topic Topic , address * PssAddress ) error {
533
+ func (p * Pss ) SetPeerPublicKey (pubkey * ecdsa.PublicKey , topic Topic , address PssAddress ) error {
534
+ if err := validateAddress (address ); err != nil {
535
+ return err
536
+ }
532
537
pubkeybytes := crypto .FromECDSAPub (pubkey )
533
538
if len (pubkeybytes ) == 0 {
534
539
return fmt .Errorf ("invalid public key: %v" , pubkey )
@@ -543,12 +548,12 @@ func (p *Pss) SetPeerPublicKey(pubkey *ecdsa.PublicKey, topic Topic, address *Ps
543
548
}
544
549
p.pubKeyPool [pubkeyid ][topic ] = psp
545
550
p .pubKeyPoolMu .Unlock ()
546
- log .Trace ("added pubkey" , "pubkeyid" , pubkeyid , "topic" , topic , "address" , common . ToHex ( * address ) )
551
+ log .Trace ("added pubkey" , "pubkeyid" , pubkeyid , "topic" , topic , "address" , address )
547
552
return nil
548
553
}
549
554
550
555
// Automatically generate a new symkey for a topic and address hint
551
- func (p * Pss ) GenerateSymmetricKey (topic Topic , address * PssAddress , addToCache bool ) (string , error ) {
556
+ func (p * Pss ) GenerateSymmetricKey (topic Topic , address PssAddress , addToCache bool ) (string , error ) {
552
557
keyid , err := p .w .GenerateSymKey ()
553
558
if err != nil {
554
559
return "" , err
@@ -569,11 +574,14 @@ func (p *Pss) GenerateSymmetricKey(topic Topic, address *PssAddress, addToCache
569
574
//
570
575
// Returns a string id that can be used to retrieve the key bytes
571
576
// from the whisper backend (see pss.GetSymmetricKey())
572
- func (p * Pss ) SetSymmetricKey (key []byte , topic Topic , address * PssAddress , addtocache bool ) (string , error ) {
577
+ func (p * Pss ) SetSymmetricKey (key []byte , topic Topic , address PssAddress , addtocache bool ) (string , error ) {
578
+ if err := validateAddress (address ); err != nil {
579
+ return "" , err
580
+ }
573
581
return p .setSymmetricKey (key , topic , address , addtocache , true )
574
582
}
575
583
576
- func (p * Pss ) setSymmetricKey (key []byte , topic Topic , address * PssAddress , addtocache bool , protected bool ) (string , error ) {
584
+ func (p * Pss ) setSymmetricKey (key []byte , topic Topic , address PssAddress , addtocache bool , protected bool ) (string , error ) {
577
585
keyid , err := p .w .AddSymKeyDirect (key )
578
586
if err != nil {
579
587
return "" , err
@@ -585,7 +593,7 @@ func (p *Pss) setSymmetricKey(key []byte, topic Topic, address *PssAddress, addt
585
593
// adds a symmetric key to the pss key pool, and optionally adds the key
586
594
// to the collection of keys used to attempt symmetric decryption of
587
595
// incoming messages
588
- func (p * Pss ) addSymmetricKeyToPool (keyid string , topic Topic , address * PssAddress , addtocache bool , protected bool ) {
596
+ func (p * Pss ) addSymmetricKeyToPool (keyid string , topic Topic , address PssAddress , addtocache bool , protected bool ) {
589
597
psp := & pssPeer {
590
598
address : address ,
591
599
protected : protected ,
@@ -601,7 +609,7 @@ func (p *Pss) addSymmetricKeyToPool(keyid string, topic Topic, address *PssAddre
601
609
p .symKeyDecryptCache [p .symKeyDecryptCacheCursor % cap (p .symKeyDecryptCache )] = & keyid
602
610
}
603
611
key , _ := p .GetSymmetricKey (keyid )
604
- log .Trace ("added symkey" , "symkeyid" , keyid , "symkey" , common .ToHex (key ), "topic" , topic , "address" , fmt . Sprintf ( "%p" , address ) , "cache" , addtocache )
612
+ log .Trace ("added symkey" , "symkeyid" , keyid , "symkey" , common .ToHex (key ), "topic" , topic , "address" , address , "cache" , addtocache )
605
613
}
606
614
607
615
// Returns a symmetric key byte seqyence stored in the whisper backend
@@ -622,7 +630,7 @@ func (p *Pss) GetPublickeyPeers(keyid string) (topic []Topic, address []PssAddre
622
630
defer p .pubKeyPoolMu .RUnlock ()
623
631
for t , peer := range p .pubKeyPool [keyid ] {
624
632
topic = append (topic , t )
625
- address = append (address , * peer .address )
633
+ address = append (address , peer .address )
626
634
}
627
635
628
636
return topic , address , nil
@@ -633,7 +641,7 @@ func (p *Pss) getPeerAddress(keyid string, topic Topic) (PssAddress, error) {
633
641
defer p .pubKeyPoolMu .RUnlock ()
634
642
if peers , ok := p .pubKeyPool [keyid ]; ok {
635
643
if t , ok := peers [topic ]; ok {
636
- return * t .address , nil
644
+ return t .address , nil
637
645
}
638
646
}
639
647
return nil , fmt .Errorf ("peer with pubkey %s, topic %x not found" , keyid , topic )
@@ -645,7 +653,7 @@ func (p *Pss) getPeerAddress(keyid string, topic Topic) (PssAddress, error) {
645
653
// encapsulating the decrypted message, and the whisper backend id
646
654
// of the symmetric key used to decrypt the message.
647
655
// It fails if decryption of the message fails or if the message is corrupted
648
- func (p * Pss ) processSym (envelope * whisper.Envelope ) (* whisper.ReceivedMessage , string , * PssAddress , error ) {
656
+ func (p * Pss ) processSym (envelope * whisper.Envelope ) (* whisper.ReceivedMessage , string , PssAddress , error ) {
649
657
metrics .GetOrRegisterCounter ("pss.process.sym" , nil ).Inc (1 )
650
658
651
659
for i := p .symKeyDecryptCacheCursor ; i > p .symKeyDecryptCacheCursor - cap (p .symKeyDecryptCache ) && i > 0 ; i -- {
@@ -677,7 +685,7 @@ func (p *Pss) processSym(envelope *whisper.Envelope) (*whisper.ReceivedMessage,
677
685
// encapsulating the decrypted message, and the byte representation of
678
686
// the public key used to decrypt the message.
679
687
// It fails if decryption of message fails, or if the message is corrupted
680
- func (p * Pss ) processAsym (envelope * whisper.Envelope ) (* whisper.ReceivedMessage , string , * PssAddress , error ) {
688
+ func (p * Pss ) processAsym (envelope * whisper.Envelope ) (* whisper.ReceivedMessage , string , PssAddress , error ) {
681
689
metrics .GetOrRegisterCounter ("pss.process.asym" , nil ).Inc (1 )
682
690
683
691
recvmsg , err := envelope .OpenAsymmetric (p .privateKey )
@@ -689,7 +697,7 @@ func (p *Pss) processAsym(envelope *whisper.Envelope) (*whisper.ReceivedMessage,
689
697
return nil , "" , nil , fmt .Errorf ("invalid message" )
690
698
}
691
699
pubkeyid := common .ToHex (crypto .FromECDSAPub (recvmsg .Src ))
692
- var from * PssAddress
700
+ var from PssAddress
693
701
p .pubKeyPoolMu .Lock ()
694
702
if p .pubKeyPool [pubkeyid ][Topic (envelope .Topic )] != nil {
695
703
from = p .pubKeyPool [pubkeyid ][Topic (envelope .Topic )].address
@@ -751,6 +759,9 @@ func (p *Pss) enqueue(msg *PssMsg) error {
751
759
//
752
760
// Will fail if raw messages are disallowed
753
761
func (p * Pss ) SendRaw (address PssAddress , topic Topic , msg []byte ) error {
762
+ if err := validateAddress (address ); err != nil {
763
+ return err
764
+ }
754
765
pssMsgParams := & msgParams {
755
766
raw : true ,
756
767
}
@@ -770,8 +781,10 @@ func (p *Pss) SendRaw(address PssAddress, topic Topic, msg []byte) error {
770
781
771
782
// if we have a proxhandler on this topic
772
783
// also deliver message to ourselves
773
- if p .isSelfPossibleRecipient (pssMsg , true ) && p .topicHandlerCaps [topic ].prox {
774
- return p .process (pssMsg , true , true )
784
+ if _ , ok := p .topicHandlerCaps [topic ]; ok {
785
+ if p .isSelfPossibleRecipient (pssMsg , true ) && p .topicHandlerCaps [topic ].prox {
786
+ return p .process (pssMsg , true , true )
787
+ }
775
788
}
776
789
return nil
777
790
}
@@ -789,11 +802,8 @@ func (p *Pss) SendSym(symkeyid string, topic Topic, msg []byte) error {
789
802
p .symKeyPoolMu .Unlock ()
790
803
if ! ok {
791
804
return fmt .Errorf ("invalid topic '%s' for symkey '%s'" , topic .String (), symkeyid )
792
- } else if psp .address == nil {
793
- return fmt .Errorf ("no address hint for topic '%s' symkey '%s'" , topic .String (), symkeyid )
794
805
}
795
- err = p .send (* psp .address , topic , msg , false , symkey )
796
- return err
806
+ return p .send (psp .address , topic , msg , false , symkey )
797
807
}
798
808
799
809
// Send a message using asymmetric encryption
@@ -808,13 +818,8 @@ func (p *Pss) SendAsym(pubkeyid string, topic Topic, msg []byte) error {
808
818
p .pubKeyPoolMu .Unlock ()
809
819
if ! ok {
810
820
return fmt .Errorf ("invalid topic '%s' for pubkey '%s'" , topic .String (), pubkeyid )
811
- } else if psp .address == nil {
812
- return fmt .Errorf ("no address hint for topic '%s' pubkey '%s'" , topic .String (), pubkeyid )
813
821
}
814
- go func () {
815
- p .send (* psp .address , topic , msg , true , common .FromHex (pubkeyid ))
816
- }()
817
- return nil
822
+ return p .send (psp .address , topic , msg , true , common .FromHex (pubkeyid ))
818
823
}
819
824
820
825
// Send is payload agnostic, and will accept any byte slice as payload
@@ -1034,3 +1039,10 @@ func (p *Pss) digestBytes(msg []byte) pssDigest {
1034
1039
copy (digest [:], key [:digestLength ])
1035
1040
return digest
1036
1041
}
1042
+
1043
+ func validateAddress (addr PssAddress ) error {
1044
+ if len (addr ) > addressLength {
1045
+ return errors .New ("address too long" )
1046
+ }
1047
+ return nil
1048
+ }
0 commit comments