From a144b895656a60946dd9a6f87a8517570f4b9269 Mon Sep 17 00:00:00 2001 From: Vlad Date: Tue, 30 Aug 2016 12:29:10 +0200 Subject: [PATCH] whisper: project restructured, version 5 introduced --- cmd/gethrpctest/main.go | 2 +- cmd/utils/flags.go | 2 +- whisper/{ => whisper02}/api.go | 2 +- whisper/{ => whisper02}/doc.go | 2 +- whisper/{ => whisper02}/envelope.go | 2 +- whisper/{ => whisper02}/envelope_test.go | 2 +- whisper/{ => whisper02}/filter.go | 2 +- whisper/{ => whisper02}/filter_test.go | 2 +- whisper/{ => whisper02}/main.go | 0 whisper/{ => whisper02}/message.go | 2 +- whisper/{ => whisper02}/message_test.go | 2 +- whisper/{ => whisper02}/peer.go | 2 +- whisper/{ => whisper02}/peer_test.go | 2 +- whisper/{ => whisper02}/topic.go | 2 +- whisper/{ => whisper02}/topic_test.go | 2 +- whisper/{ => whisper02}/whisper.go | 2 +- whisper/{ => whisper02}/whisper_test.go | 2 +- whisper/whisper05/api.go | 338 ++++++++++++++++++++++ whisper/whisper05/doc.go | 69 +++++ whisper/whisper05/envelope.go | 207 ++++++++++++++ whisper/whisper05/filter.go | 193 +++++++++++++ whisper/whisper05/message.go | 339 +++++++++++++++++++++++ whisper/whisper05/peer.go | 175 ++++++++++++ whisper/whisper05/topic.go | 92 ++++++ whisper/whisper05/whisper.go | 333 ++++++++++++++++++++++ 25 files changed, 1762 insertions(+), 16 deletions(-) rename whisper/{ => whisper02}/api.go (99%) rename whisper/{ => whisper02}/doc.go (98%) rename whisper/{ => whisper02}/envelope.go (99%) rename whisper/{ => whisper02}/envelope_test.go (99%) rename whisper/{ => whisper02}/filter.go (99%) rename whisper/{ => whisper02}/filter_test.go (99%) rename whisper/{ => whisper02}/main.go (100%) rename whisper/{ => whisper02}/message.go (99%) rename whisper/{ => whisper02}/message_test.go (99%) rename whisper/{ => whisper02}/peer.go (99%) rename whisper/{ => whisper02}/peer_test.go (99%) rename whisper/{ => whisper02}/topic.go (99%) rename whisper/{ => whisper02}/topic_test.go (99%) rename whisper/{ => whisper02}/whisper.go (99%) rename whisper/{ => whisper02}/whisper_test.go (99%) create mode 100644 whisper/whisper05/api.go create mode 100644 whisper/whisper05/doc.go create mode 100644 whisper/whisper05/envelope.go create mode 100644 whisper/whisper05/filter.go create mode 100644 whisper/whisper05/message.go create mode 100644 whisper/whisper05/peer.go create mode 100644 whisper/whisper05/topic.go create mode 100644 whisper/whisper05/whisper.go diff --git a/cmd/gethrpctest/main.go b/cmd/gethrpctest/main.go index d267dbf585..5e2b9cde3d 100644 --- a/cmd/gethrpctest/main.go +++ b/cmd/gethrpctest/main.go @@ -32,7 +32,7 @@ import ( "github.com/ethereum/go-ethereum/node" "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/tests" - "github.com/ethereum/go-ethereum/whisper" + whisper "github.com/ethereum/go-ethereum/whisper/whisper02" ) const defaultTestKey = "b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291" diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index a1f098f299..7b9710c1a4 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -48,7 +48,7 @@ import ( "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/pow" "github.com/ethereum/go-ethereum/rpc" - "github.com/ethereum/go-ethereum/whisper" + whisper "github.com/ethereum/go-ethereum/whisper/whisper02" "gopkg.in/urfave/cli.v1" ) diff --git a/whisper/api.go b/whisper/whisper02/api.go similarity index 99% rename from whisper/api.go rename to whisper/whisper02/api.go index d1f454a1fa..e1b28468d9 100644 --- a/whisper/api.go +++ b/whisper/whisper02/api.go @@ -14,7 +14,7 @@ // You should have received a copy of the GNU Lesser General Public License // along with the go-ethereum library. If not, see . -package whisper +package whisper02 import ( "encoding/json" diff --git a/whisper/doc.go b/whisper/whisper02/doc.go similarity index 98% rename from whisper/doc.go rename to whisper/whisper02/doc.go index cfb0b51175..d2603650c7 100644 --- a/whisper/doc.go +++ b/whisper/whisper02/doc.go @@ -29,4 +29,4 @@ Whisper is a pure identity-based messaging system. Whisper provides a low-level or prejudiced by the low-level hardware attributes and characteristics, particularly the notion of singular endpoints. */ -package whisper +package whisper02 diff --git a/whisper/envelope.go b/whisper/whisper02/envelope.go similarity index 99% rename from whisper/envelope.go rename to whisper/whisper02/envelope.go index 97d489b961..b1d74c9d61 100644 --- a/whisper/envelope.go +++ b/whisper/whisper02/envelope.go @@ -17,7 +17,7 @@ // Contains the Whisper protocol Envelope element. For formal details please see // the specs at https://github.com/ethereum/wiki/wiki/Whisper-PoC-1-Protocol-Spec#envelopes. -package whisper +package whisper02 import ( "crypto/ecdsa" diff --git a/whisper/envelope_test.go b/whisper/whisper02/envelope_test.go similarity index 99% rename from whisper/envelope_test.go rename to whisper/whisper02/envelope_test.go index 3bfe527372..c7172024f7 100644 --- a/whisper/envelope_test.go +++ b/whisper/whisper02/envelope_test.go @@ -14,7 +14,7 @@ // You should have received a copy of the GNU Lesser General Public License // along with the go-ethereum library. If not, see . -package whisper +package whisper02 import ( "bytes" diff --git a/whisper/filter.go b/whisper/whisper02/filter.go similarity index 99% rename from whisper/filter.go rename to whisper/whisper02/filter.go index 9f6d6b7817..5a2108fa6d 100644 --- a/whisper/filter.go +++ b/whisper/whisper02/filter.go @@ -16,7 +16,7 @@ // Contains the message filter for fine grained subscriptions. -package whisper +package whisper02 import ( "crypto/ecdsa" diff --git a/whisper/filter_test.go b/whisper/whisper02/filter_test.go similarity index 99% rename from whisper/filter_test.go rename to whisper/whisper02/filter_test.go index b805b2baf1..7bbaa328f6 100644 --- a/whisper/filter_test.go +++ b/whisper/whisper02/filter_test.go @@ -14,7 +14,7 @@ // You should have received a copy of the GNU Lesser General Public License // along with the go-ethereum library. If not, see . -package whisper +package whisper02 import ( "bytes" diff --git a/whisper/main.go b/whisper/whisper02/main.go similarity index 100% rename from whisper/main.go rename to whisper/whisper02/main.go diff --git a/whisper/message.go b/whisper/whisper02/message.go similarity index 99% rename from whisper/message.go rename to whisper/whisper02/message.go index f05b5d8b54..a3f59a690e 100644 --- a/whisper/message.go +++ b/whisper/whisper02/message.go @@ -17,7 +17,7 @@ // Contains the Whisper protocol Message element. For formal details please see // the specs at https://github.com/ethereum/wiki/wiki/Whisper-PoC-1-Protocol-Spec#messages. -package whisper +package whisper02 import ( "crypto/ecdsa" diff --git a/whisper/message_test.go b/whisper/whisper02/message_test.go similarity index 99% rename from whisper/message_test.go rename to whisper/whisper02/message_test.go index 921c967a96..e87238d0b7 100644 --- a/whisper/message_test.go +++ b/whisper/whisper02/message_test.go @@ -14,7 +14,7 @@ // You should have received a copy of the GNU Lesser General Public License // along with the go-ethereum library. If not, see . -package whisper +package whisper02 import ( "bytes" diff --git a/whisper/peer.go b/whisper/whisper02/peer.go similarity index 99% rename from whisper/peer.go rename to whisper/whisper02/peer.go index ee10e66e79..3e108816d3 100644 --- a/whisper/peer.go +++ b/whisper/whisper02/peer.go @@ -14,7 +14,7 @@ // You should have received a copy of the GNU Lesser General Public License // along with the go-ethereum library. If not, see . -package whisper +package whisper02 import ( "fmt" diff --git a/whisper/peer_test.go b/whisper/whisper02/peer_test.go similarity index 99% rename from whisper/peer_test.go rename to whisper/whisper02/peer_test.go index 636bd8ca1f..2ec7a51c15 100644 --- a/whisper/peer_test.go +++ b/whisper/whisper02/peer_test.go @@ -14,7 +14,7 @@ // You should have received a copy of the GNU Lesser General Public License // along with the go-ethereum library. If not, see . -package whisper +package whisper02 import ( "testing" diff --git a/whisper/topic.go b/whisper/whisper02/topic.go similarity index 99% rename from whisper/topic.go rename to whisper/whisper02/topic.go index d37eb25ee1..2a80df4884 100644 --- a/whisper/topic.go +++ b/whisper/whisper02/topic.go @@ -17,7 +17,7 @@ // Contains the Whisper protocol Topic element. For formal details please see // the specs at https://github.com/ethereum/wiki/wiki/Whisper-PoC-1-Protocol-Spec#topics. -package whisper +package whisper02 import "github.com/ethereum/go-ethereum/crypto" diff --git a/whisper/topic_test.go b/whisper/whisper02/topic_test.go similarity index 99% rename from whisper/topic_test.go rename to whisper/whisper02/topic_test.go index 9c45f67408..21f5e55b01 100644 --- a/whisper/topic_test.go +++ b/whisper/whisper02/topic_test.go @@ -14,7 +14,7 @@ // You should have received a copy of the GNU Lesser General Public License // along with the go-ethereum library. If not, see . -package whisper +package whisper02 import ( "bytes" diff --git a/whisper/whisper.go b/whisper/whisper02/whisper.go similarity index 99% rename from whisper/whisper.go rename to whisper/whisper02/whisper.go index 0a49c1000b..f0f38f29ab 100644 --- a/whisper/whisper.go +++ b/whisper/whisper02/whisper.go @@ -14,7 +14,7 @@ // You should have received a copy of the GNU Lesser General Public License // along with the go-ethereum library. If not, see . -package whisper +package whisper02 import ( "crypto/ecdsa" diff --git a/whisper/whisper_test.go b/whisper/whisper02/whisper_test.go similarity index 99% rename from whisper/whisper_test.go rename to whisper/whisper02/whisper_test.go index 094df373c2..42e2935732 100644 --- a/whisper/whisper_test.go +++ b/whisper/whisper02/whisper_test.go @@ -14,7 +14,7 @@ // You should have received a copy of the GNU Lesser General Public License // along with the go-ethereum library. If not, see . -package whisper +package whisper02 import ( "testing" diff --git a/whisper/whisper05/api.go b/whisper/whisper05/api.go new file mode 100644 index 0000000000..91522908ca --- /dev/null +++ b/whisper/whisper05/api.go @@ -0,0 +1,338 @@ +// Copyright 2015 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package whisper05 + +import ( + "encoding/json" + "errors" + "fmt" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/logger/glog" + "github.com/ethereum/go-ethereum/rpc" +) + +// PublicWhisperAPI provides the whisper RPC service. +type PublicWhisperAPI struct { + whisper *Whisper +} + +// NewPublicWhisperAPI create a new RPC whisper service. +func NewPublicWhisperAPI(w *Whisper) *PublicWhisperAPI { + return &PublicWhisperAPI{whisper: w} +} + +// Version returns the Whisper version this node offers. +func (self *PublicWhisperAPI) Version() (*rpc.HexNumber, error) { + if self.whisper == nil { + return rpc.NewHexNumber(0), whisperOffLineErr + } + return rpc.NewHexNumber(self.whisper.Version()), nil +} + +// HasIdentity checks if the the whisper node is configured with the private key +// of the specified public pair. +func (self *PublicWhisperAPI) HasIdentity(identity string) (bool, error) { + if self.whisper == nil { + return false, whisperOffLineErr + } + return self.whisper.HasIdentity(crypto.ToECDSAPub(common.FromHex(identity))), nil +} + +// NewIdentity generates a new cryptographic identity for the client, and injects +// it into the known identities for message decryption. +func (self *PublicWhisperAPI) NewIdentity() (string, error) { + if self.whisper == nil { + return "", whisperOffLineErr + } + identity := self.whisper.NewIdentity() + return common.ToHex(crypto.FromECDSAPub(&identity.PublicKey)), nil +} + +// NewWhisperFilter creates and registers a new message filter to watch for inbound whisper messages. +func (self *PublicWhisperAPI) NewFilter(args WhisperFilterArgs) (*rpc.HexNumber, error) { + if self.whisper == nil { + return nil, whisperOffLineErr + } + + filter := Filter{ + Src: crypto.ToECDSAPub(args.From), + Dst: crypto.ToECDSAPub(args.To), + KeySym: args.KeySym, + PoW: args.PoW, + messages: make(map[common.Hash]*ReceivedMessage), + } + + if len(args.Topics) > 0 { + for _, x := range args.Topics { + filter.Topics = append(filter.Topics, x) + } + } + + // todo: add glog on error everywhere + + if len(args.To) == 0 && len(args.KeySym) == 0 { + info := "NewFilter: filter must contain either symmetric or asymmetric key" + glog.V(logger.Error).Infof(info) + return nil, errors.New(info) + } + + if len(args.KeySym) > 0 && len(args.Topics) == 0 { + return nil, fmt.Errorf("NewFilter: topic encryption require at least one topic") + } + + if len(args.KeySym) > 0 { + filter.TopicKeyHash = crypto.Keccak256Hash(filter.KeySym) + if len(args.Topics) > 0 { + // if Topics are not provided, just use the default derivation function + t := DeriveTopicFromSymmetricKey(args.KeySym) + filter.Topics = append(filter.Topics, t) + } + } + + if len(args.To) > 0 { + if !validatePublicKey(filter.Dst) { + return nil, fmt.Errorf("NewFilter: Invalid 'To' address") + } + filter.KeyAsym = self.whisper.GetIdentity(filter.Dst) + if filter.KeyAsym == nil { + info := "NewFilter: non-existent identity provided" + glog.V(logger.Error).Infof(info) + return nil, errors.New(info) + } + } + + if len(args.From) > 0 { + if !validatePublicKey(filter.Src) { + return nil, fmt.Errorf("NewFilter: Invalid 'From' address") + } + } + + id := self.whisper.Watch(&filter) + return rpc.NewHexNumber(id), nil +} + +// UninstallFilter disables and removes an existing filter. +func (self *PublicWhisperAPI) UninstallFilter(filterId rpc.HexNumber) { + self.whisper.Unwatch(filterId.Int()) +} + +// GetFilterChanges retrieves all the new messages matched by a filter since the last retrieval. +func (self *PublicWhisperAPI) GetFilterChanges(filterId rpc.HexNumber) []WhisperMessage { + f := self.whisper.filters.Get(filterId.Int()) + if f != nil { + newMail := f.retrieve() + return toWhisperMessages(newMail) + } + return toWhisperMessages(nil) +} + +// GetMessages retrieves all the known messages that match a specific filter. +func (self *PublicWhisperAPI) GetMessages(filterId rpc.HexNumber) []WhisperMessage { + all := self.whisper.Messages(filterId.Int()) + return toWhisperMessages(all) +} + +// toWhisperMessages converts a Whisper message to a RPC whisper message. +func toWhisperMessages(messages []*ReceivedMessage) []WhisperMessage { + msgs := make([]WhisperMessage, len(messages)) + for i, msg := range messages { + msgs[i] = NewWhisperMessage(msg) + } + return msgs +} + +// Post injects a message into the whisper network for distribution. +func (self *PublicWhisperAPI) Post(args PostArgs) error { + if self.whisper == nil { + return whisperOffLineErr + } + + message := NewSentMessage(args.Payload) + options := Options{ + TTL: args.TTL, + Dst: crypto.ToECDSAPub(args.To), + KeySym: args.KeySym, + Topic: args.Topic, + Pading: args.Padding, + WorkTime: args.WorkTime, + PoW: args.PoW, + } + + // todo: add glog on error everywhere + + if len(args.KeySym) > 0 && (args.Topic == TopicType{}) { + // if Topic is not provided, just use the default derivation function + options.Topic = DeriveTopicFromSymmetricKey(args.KeySym) + } + + if len(args.From) > 0 { + pub := crypto.ToECDSAPub(args.From) + if !validatePublicKey(pub) { + return fmt.Errorf("Post: Invalid 'From' address") + } + options.Src = self.whisper.GetIdentity(pub) + if options.Src == nil { + info := "Post: non-existent identity provided" + glog.V(logger.Error).Infof(info) + return errors.New(info) + } + } + + if len(args.To) == 0 && len(args.KeySym) == 0 { + info := "Post: message must be encrypted either symmetrically or asymmetrically" + glog.V(logger.Error).Infof(info) + return errors.New(info) + } + + if len(args.To) > 0 { + if !validatePublicKey(options.Dst) { + return fmt.Errorf("Post: Invalid 'To' address") + } + } + + envelope, err := message.Wrap(options) + if err != nil { + return err + } + return self.whisper.Send(envelope) +} + +type PostArgs struct { + TTL uint32 `json:"ttl"` + From rpc.HexBytes `json:"from"` + To rpc.HexBytes `json:"to"` + KeySym rpc.HexBytes `json:"key"` + Topic TopicType `json:"topic"` + Padding rpc.HexBytes `json:"padding"` + Payload rpc.HexBytes `json:"payload"` + WorkTime uint32 `json:"worktime"` + PoW float64 `json:"pow"` +} + +func (args *PostArgs) UnmarshalJSON(data []byte) (err error) { + var obj struct { + TTL uint32 `json:"ttl"` + From rpc.HexBytes `json:"from"` + To rpc.HexBytes `json:"to"` + Key rpc.HexBytes `json:"key"` + Topic TopicType `json:"topic"` + Payload rpc.HexBytes `json:"payload"` + Padding rpc.HexBytes `json:"padding"` + WorkTime uint32 `json:"worktime"` + PoW float64 `json:"pow"` + } + + if err := json.Unmarshal(data, &obj); err != nil { + return err + } + + args.TTL = obj.TTL + args.From = obj.From + args.To = obj.To + args.KeySym = obj.Key + args.Topic = obj.Topic + args.Payload = obj.Payload + args.Padding = obj.Padding + args.WorkTime = obj.WorkTime + args.PoW = obj.PoW + + return nil +} + +type WhisperFilterArgs struct { + To []byte + From []byte + KeySym []byte + PoW float64 + Topics []TopicType +} + +// UnmarshalJSON implements the json.Unmarshaler interface, invoked to convert a +// JSON message blob into a WhisperFilterArgs structure. +func (args *WhisperFilterArgs) UnmarshalJSON(b []byte) (err error) { + // Unmarshal the JSON message and sanity check + var obj struct { + To rpc.HexBytes `json:"to"` + From rpc.HexBytes `json:"from"` + Key rpc.HexBytes `json:"key"` + PoW float64 `json:"pow"` + Topics []interface{} `json:"topics"` + } + if err := json.Unmarshal(b, &obj); err != nil { + return err + } + + args.To = obj.To + args.From = obj.From + args.KeySym = obj.Key + args.PoW = obj.PoW + + // Construct the topic array + if obj.Topics != nil { + topics := make([]string, len(obj.Topics)) + for i, field := range obj.Topics { + switch value := field.(type) { + case string: + topics[i] = value + case nil: + return fmt.Errorf("topic[%d] is empty", i) + default: + return fmt.Errorf("topic[%d] is not a string", i) + } + } + topicsDecoded := make([]TopicType, len(topics)) + for j, s := range topics { + x := common.FromHex(s) + if x == nil || len(x) != topicLength { + return fmt.Errorf("topic[%d] is invalid", j) + } + topicsDecoded[j] = BytesToTopic(x) + } + args.Topics = topicsDecoded + } + + return nil +} + +// WhisperMessage is the RPC representation of a whisper message. +type WhisperMessage struct { + Payload string `json:"payload"` + Padding string `json:"padding"` + From string `json:"from"` + To string `json:"to"` + Sent uint32 `json:"sent"` + TTL uint32 `json:"ttl"` + PoW float64 `json:"pow"` + Hash string `json:"hash"` +} + +// NewWhisperMessage converts an internal message into an API version. +func NewWhisperMessage(message *ReceivedMessage) WhisperMessage { + return WhisperMessage{ + Payload: common.ToHex(message.Payload), + Padding: common.ToHex(message.Padding), + From: common.ToHex(crypto.FromECDSAPub(message.Recover())), + To: common.ToHex(crypto.FromECDSAPub(message.Dst)), + Sent: message.Sent, + TTL: message.TTL, + PoW: message.PoW, + Hash: common.ToHex(message.EnvelopeHash.Bytes()), + } +} diff --git a/whisper/whisper05/doc.go b/whisper/whisper05/doc.go new file mode 100644 index 0000000000..d92cafb092 --- /dev/null +++ b/whisper/whisper05/doc.go @@ -0,0 +1,69 @@ +// Copyright 2014 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +/* +Package whisper implements the Whisper PoC-1. + +(https://github.com/ethereum/wiki/wiki/Whisper-PoC-1-Protocol-Spec) + +Whisper combines aspects of both DHTs and datagram messaging systems (e.g. UDP). +As such it may be likened and compared to both, not dissimilar to the +matter/energy duality (apologies to physicists for the blatant abuse of a +fundamental and beautiful natural principle). + +Whisper is a pure identity-based messaging system. Whisper provides a low-level +(non-application-specific) but easily-accessible API without being based upon +or prejudiced by the low-level hardware attributes and characteristics, +particularly the notion of singular endpoints. +*/ +package whisper05 + +import ( + "time" +) + +const ( + statusCode = 0x00 + messagesCode = 0x01 + + protocolVersion uint64 = 5 + protocolVersionStr = "5.0" + protocolName = "shh" + + signatureFlag = byte(1 << 7) + paddingFlag = byte(1 << 6) + + topicLength = 4 + signatureLength = 65 + maxPadLength = 255 + aesKeyLength = 32 + saltLength = 12 + kdfIterations = 4096 + msgMaxLength = 0xFFFF + + expirationCycle = 800 * time.Millisecond + transmissionCycle = 300 * time.Millisecond + + DefaultTTL = 50 // seconds +) + +type whisperOfflineError struct{} + +var whisperOffLineErr = new(whisperOfflineError) + +func (e *whisperOfflineError) Error() string { + return "whisper is offline" +} diff --git a/whisper/whisper05/envelope.go b/whisper/whisper05/envelope.go new file mode 100644 index 0000000000..1312578049 --- /dev/null +++ b/whisper/whisper05/envelope.go @@ -0,0 +1,207 @@ +// Copyright 2014 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +// Contains the Whisper protocol Envelope element. For formal details please see +// the specs at https://github.com/ethereum/wiki/wiki/Whisper-PoC-1-Protocol-Spec#envelopes. + +package whisper05 + +import ( + "crypto/ecdsa" + "encoding/binary" + "fmt" + "time" + + "math" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/crypto/ecies" + "github.com/ethereum/go-ethereum/rlp" +) + +// Envelope represents a clear-text data packet to transmit through the Whisper +// network. Its contents may or may not be encrypted and signed. +type Envelope struct { + Expiry uint32 + TTL uint32 + Topic TopicType + Salt []byte + AESNonce []byte + Data []byte + EnvNonce uint64 + + hash common.Hash // Cached hash of the envelope to avoid rehashing every time + pow float64 // Message-specific PoW as described in the Whisper specification +} + +// NewEnvelope wraps a Whisper message with expiration and destination data +// included into an envelope for network forwarding. +func NewEnvelope(ttl uint32, topic TopicType, salt []byte, aesNonce []byte, msg *SentMessage) *Envelope { + return &Envelope{ + Expiry: uint32(time.Now().Add(time.Second * time.Duration(ttl)).Unix()), + TTL: ttl, + Topic: topic, + Salt: salt, + AESNonce: aesNonce, + Data: msg.Raw, + EnvNonce: 0, + } +} + +func (self *Envelope) isSymmetric() bool { + return self.AESNonce != nil +} + +func (self *Envelope) isAsymmetric() bool { + return !self.isSymmetric() +} + +// Seal closes the envelope by spending the requested amount of time as a proof +// of work on hashing the data. +func (self *Envelope) Seal(options Options) { + var target int + if options.PoW == 0 { + // adjust for the duration of Seal() execution only if execution time is predefined unconditionally + self.Expiry += options.WorkTime + } else { + target = self.powToFirstBit(options.PoW) + } + + buf := make([]byte, 64) + h := crypto.Keccak256(self.rlpWithoutNonce()) + copy(buf[:32], h) + + finish, bestBit := time.Now().Add(time.Duration(options.WorkTime)*time.Second).UnixNano(), 0 + for nonce := uint64(0); time.Now().UnixNano() < finish; { + for i := 0; i < 1024; i++ { + binary.BigEndian.PutUint64(buf[56:], nonce) + h = crypto.Keccak256(buf) + firstBit := common.FirstBitSet(common.BigD(h)) + if firstBit > bestBit { + self.EnvNonce, bestBit = nonce, firstBit + if target > 0 && bestBit >= target { + return + } + } + nonce++ + } + } +} + +func (self *Envelope) calculatePoW() float64 { + h := self.Hash() + firstBit := common.FirstBitSet(common.BigD(h.Bytes())) + x := math.Pow(2, float64(firstBit)) + x /= float64(len(self.Data)) + x /= float64(self.TTL) + return x +} + +func (self *Envelope) powToFirstBit(pow float64) int { + x := pow + x *= float64(len(self.Data)) + x *= float64(self.TTL) + bits := math.Log2(x) + bits = math.Ceil(bits) + return int(bits) +} + +// rlpWithoutNonce returns the RLP encoded envelope contents, except the nonce. +func (self *Envelope) rlpWithoutNonce() []byte { + enc, _ := rlp.EncodeToBytes([]interface{}{self.Expiry, self.TTL, self.Topic, self.Salt, self.AESNonce, self.Data}) + return enc +} + +// Hash returns the SHA3 hash of the envelope, calculating it if not yet done. +func (self *Envelope) Hash() common.Hash { + if (self.hash == common.Hash{}) { + enc, _ := rlp.EncodeToBytes(self) + self.hash = crypto.Keccak256Hash(enc) + } + return self.hash +} + +// DecodeRLP decodes an Envelope from an RLP data stream. +func (self *Envelope) DecodeRLP(s *rlp.Stream) error { + raw, err := s.Raw() + if err != nil { + return err + } + // The decoding of Envelope uses the struct fields but also needs + // to compute the hash of the whole RLP-encoded envelope. This + // type has the same structure as Envelope but is not an + // rlp.Decoder (does not implement DecodeRLP function). + // Only public members will be encoded. + type rlpenv Envelope + if err := rlp.DecodeBytes(raw, (*rlpenv)(self)); err != nil { + return err + } + self.hash = crypto.Keccak256Hash(raw) + return nil +} + +// OpenAsymmetric tries to decrypt an envelope, potentially encrypted with a particular key. +func (self *Envelope) OpenAsymmetric(key *ecdsa.PrivateKey) (*ReceivedMessage, error) { + message := &ReceivedMessage{Raw: self.Data} + err := message.decryptAsymmetric(key) + switch err { + case nil: + return message, nil + case ecies.ErrInvalidPublicKey: // addressed to somebody else + return nil, err + default: + return nil, fmt.Errorf("unable to open envelope, decrypt failed: %v", err) + } +} + +// OpenSymmetric tries to decrypt an envelope, potentially encrypted with a particular key. +func (self *Envelope) OpenSymmetric(key []byte) (msg *ReceivedMessage, err error) { + msg = &ReceivedMessage{Raw: self.Data} + err = msg.decryptSymmetric(key, self.Salt, self.AESNonce) + if err != nil { + msg = nil + } + return +} + +// Open tries to decrypt an envelope, and populates the message fields in case of success. +func (self *Envelope) Open(watcher *Filter) (msg *ReceivedMessage) { + if self.isAsymmetric() { + msg, _ = self.OpenAsymmetric(watcher.KeyAsym) + if msg != nil { + msg.Dst = watcher.Dst + } + } else if self.isSymmetric() { + msg, _ = self.OpenSymmetric(watcher.KeySym) + if msg != nil { + msg.TopicKeyHash = crypto.Keccak256Hash(watcher.KeySym) + } + } + + if msg != nil { + ok := msg.Validate() + if !ok { + return nil + } + msg.Topic = self.Topic + msg.PoW = self.pow + msg.TTL = self.TTL + msg.Sent = self.Expiry - self.TTL + msg.EnvelopeHash = self.hash + } + return msg +} diff --git a/whisper/whisper05/filter.go b/whisper/whisper05/filter.go new file mode 100644 index 0000000000..af2f66ee74 --- /dev/null +++ b/whisper/whisper05/filter.go @@ -0,0 +1,193 @@ +package whisper05 + +import ( + "crypto/ecdsa" + + "sync" + + "github.com/ethereum/go-ethereum/common" +) + +type Filter struct { + Src *ecdsa.PublicKey // Sender of the message + Dst *ecdsa.PublicKey // Recipient of the message + KeyAsym *ecdsa.PrivateKey // Private Key of recipient + Topics []TopicType // Topics to filter messages with + KeySym []byte // Key associated with the Topic + TopicKeyHash common.Hash // The Keccak256Hash of the symmetric key + PoW float64 // Proof of work as described in the Whisper spec + + messages map[common.Hash]*ReceivedMessage + mutex sync.RWMutex +} + +type Filters struct { + id int + watchers map[int]*Filter + ch chan Envelope + quit chan struct{} + whisper *Whisper +} + +func NewFilters(w *Whisper) *Filters { + return &Filters{ + ch: make(chan Envelope), + watchers: make(map[int]*Filter), + quit: make(chan struct{}), + whisper: w, + } +} + +func (self *Filters) Start() { + go self.loop() +} + +func (self *Filters) Stop() { + close(self.quit) +} + +func (self *Filters) Notify(env *Envelope) { + self.ch <- *env +} + +func (self *Filters) Install(watcher *Filter) int { + self.watchers[self.id] = watcher + ret := self.id + self.id++ + return ret +} + +func (self *Filters) Uninstall(id int) { + delete(self.watchers, id) +} + +func (self *Filters) Get(i int) *Filter { + return self.watchers[i] +} + +func (self *Filters) loop() { + for { + select { + case <-self.quit: + return + case envelope := <-self.ch: + self.processEnvelope(&envelope) + } + } +} + +func (self *Filters) processEnvelope(envelope *Envelope) { + var msg *ReceivedMessage + for _, watcher := range self.watchers { + match := false + if msg != nil { + match = watcher.MatchMessage(msg) + } else { + match = watcher.MatchEnvelope(envelope) + if match { + msg = envelope.Open(watcher) + } + } + + if match && msg != nil { + watcher.Trigger(msg) + } + } + + if msg != nil { + go self.whisper.addDecryptedMessage(msg) + } +} + +func (self *Filter) expectsAsymmetricEncryption() bool { + return self.KeyAsym != nil +} + +func (self *Filter) expectsSymmetricEncryption() bool { + return self.KeySym != nil +} + +func (self *Filter) Trigger(msg *ReceivedMessage) { + self.mutex.Lock() + defer self.mutex.Unlock() + + if _, exist := self.messages[msg.EnvelopeHash]; !exist { + self.messages[msg.EnvelopeHash] = msg + } +} + +func (self *Filter) retrieve() (all []*ReceivedMessage) { + self.mutex.RLock() + defer self.mutex.RUnlock() + + all = make([]*ReceivedMessage, 0, len(self.messages)) + for _, msg := range self.messages { + all = append(all, msg) + } + self.messages = make(map[common.Hash]*ReceivedMessage) // delete old messages + return all +} + +func (self *Filter) MatchMessage(msg *ReceivedMessage) bool { + if self.PoW > 0 && msg.PoW < self.PoW { + return false + } + + if self.Src != nil && !isEqual(msg.Src, self.Src) { + return false + } + + if self.expectsAsymmetricEncryption() && msg.isAsymmetricEncryption() { + // if Dst match, ignore the topic + return isEqual(self.Dst, msg.Dst) + } else if self.expectsSymmetricEncryption() && msg.isSymmetricEncryption() { + // we need to compare the keys (or rather thier hashes), because of + // possible collision (different keys can produce the same topic). + // we also need to compare the topics, because they could be arbitrary (not related to KeySym). + if self.TopicKeyHash == msg.TopicKeyHash { + for _, t := range self.Topics { + if t == msg.Topic { + return true + } + } + return false + } + } + return false +} + +func (self *Filter) MatchEnvelope(envelope *Envelope) bool { + if self.PoW > 0 && envelope.pow < self.PoW { + return false + } + + encryptionMethodMatch := false + if self.expectsAsymmetricEncryption() && envelope.isAsymmetric() { + encryptionMethodMatch = true + if self.Topics == nil { + return true // wildcard + } + } else if self.expectsSymmetricEncryption() && envelope.isSymmetric() { + encryptionMethodMatch = true + } + + if encryptionMethodMatch { + for _, t := range self.Topics { + if t == envelope.Topic { + return true + } + } + } + + return false +} + +func isEqual(a, b *ecdsa.PublicKey) bool { + if !validatePublicKey(a) { + return false + } else if !validatePublicKey(b) { + return false + } + // the Curve is always the same, just compare the points + return a.X.Cmp(b.X) == 0 && a.Y.Cmp(b.Y) == 0 +} diff --git a/whisper/whisper05/message.go b/whisper/whisper05/message.go new file mode 100644 index 0000000000..aa28bde7cc --- /dev/null +++ b/whisper/whisper05/message.go @@ -0,0 +1,339 @@ +// Copyright 2014 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +// Contains the Whisper protocol Message element. For formal details please see +// the specs at https://github.com/ethereum/wiki/wiki/Whisper-PoC-1-Protocol-Spec#messages. +// todo: fix the spec link, and move it to doc.go + +package whisper05 + +import ( + crand "crypto/rand" + "errors" + + "crypto/aes" + "crypto/cipher" + "crypto/ecdsa" + "crypto/sha256" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/logger/glog" + "golang.org/x/crypto/pbkdf2" +) + +// Options specifies the exact way a message should be wrapped into an Envelope. +type Options struct { + TTL uint32 + Src *ecdsa.PrivateKey + Dst *ecdsa.PublicKey + KeySym []byte + Topic TopicType + Pading []byte + WorkTime uint32 + PoW float64 +} + +// SentMessage represents an end-user data packet to transmit through the +// Whisper protocol. These are wrapped into Envelopes that need not be +// understood by intermediate nodes, just forwarded. +type SentMessage struct { + Raw []byte +} + +// ReceivedMessage represents a data packet to be received through the +// Whisper protocol. +type ReceivedMessage struct { + Raw []byte + + Payload []byte + Padding []byte + Signature []byte + + PoW float64 // Proof of work as described in the Whisper spec + Sent uint32 // Time when the message was posted into the network + TTL uint32 // Maximum time to live allowed for the message + Src *ecdsa.PublicKey // Message recipient (identity used to decode the message) + Dst *ecdsa.PublicKey // Message recipient (identity used to decode the message) + Topic TopicType + TopicKeyHash common.Hash // The Keccak256Hash of the key, associated with the Topic + EnvelopeHash common.Hash // Message envelope hash to act as a unique id +} + +func DeriveTopicFromSymmetricKey(key []byte) TopicType { + // todo: it is not secure enough, use kdf instead + hash := crypto.Keccak256Hash(key) + return HashToTopic(hash) +} + +func isMessageSigned(flags byte) bool { + return (flags & signatureFlag) != 0 +} + +func isMessagePadded(flags byte) bool { + return (flags & paddingFlag) != 0 +} + +func (self *ReceivedMessage) isSymmetricEncryption() bool { + return self.TopicKeyHash != common.Hash{} +} + +func (self *ReceivedMessage) isAsymmetricEncryption() bool { + return self.Dst != nil +} + +// NewMessage creates and initializes a non-signed, non-encrypted Whisper message. +func NewSentMessage(payload []byte) *SentMessage { + // Construct an initial flag set: no signature, no padding, other bits random + buf := make([]byte, 1) + crand.Read(buf) + flags := buf[0] + flags &= ^signatureFlag + flags &= ^paddingFlag + + msg := SentMessage{} + msg.Raw = make([]byte, 1, len(payload)+signatureLength+maxPadLength+1) + msg.Raw[0] = flags + msg.Raw = append(msg.Raw, payload...) + return &msg +} + +// appendPadding appends the pseudorandom padding bytes and sets the padding flag. +// The last byte contains the size of padding (thus, its size must not exceed 256). +func (self *SentMessage) appendPadding(options Options) { + if isMessageSigned(self.Raw[0]) { + // this should not happen, but no reason to panic + glog.V(logger.Error).Infof("Trying to pad a message which was already signed") + return + } else if isMessagePadded(self.Raw[0]) { + // this should not happen, but no reason to panic + glog.V(logger.Error).Infof("Trying to pad a message which was already padded") + return + } + + total := len(self.Raw) + if options.Src != nil { + total += signatureLength + } + odd := total % maxPadLength + if odd > 0 { + padSize := maxPadLength - odd + buf := make([]byte, padSize) + crand.Read(buf) + if options.Pading != nil { + copy(buf, options.Pading) + } + buf[padSize-1] = byte(padSize) + self.Raw = append(self.Raw, buf...) + self.Raw[0] |= paddingFlag + } +} + +// sign calculates and sets the cryptographic signature for the message, +// also setting the sign flag. +func (self *SentMessage) sign(key *ecdsa.PrivateKey) (err error) { + if isMessageSigned(self.Raw[0]) { + // this should not happen, but no reason to panic + glog.V(logger.Error).Infof("Trying to sign a message which was already signed") + return + } + hash := crypto.Keccak256(self.Raw) + signature, err := crypto.Sign(hash, key) + if err != nil { + self.Raw = append(self.Raw, signature...) + self.Raw[0] |= signatureFlag + } + return +} + +// encryptAsymmetric encrypts a message with a public key. +func (self *SentMessage) encryptAsymmetric(key *ecdsa.PublicKey) error { + encrypted, err := crypto.Encrypt(key, self.Raw) + if err == nil { + self.Raw = encrypted + } + return err +} + +// encryptSymmetric encrypts a message with a topic key, using AES-GCM-256. +// nonce size should be 12 bytes (see cipher.gcmStandardNonceSize). +func (self *SentMessage) encryptSymmetric(key []byte) (salt []byte, nonce []byte, err error) { + salt = make([]byte, saltLength) + _, err = crand.Read(salt) + if err != nil { + return + } + + derivedKey := pbkdf2.Key(key, salt, kdfIterations, aesKeyLength, sha256.New) + + block, err := aes.NewCipher(derivedKey) + if err != nil { + return + } + aesgcm, err := cipher.NewGCM(block) + if err != nil { + return + } + + // never use more than 2^32 random nonces with a given key + nonce = make([]byte, aesgcm.NonceSize()) + _, err = crand.Read(nonce) + if err != nil { + return + } + self.Raw = aesgcm.Seal(nil, nonce, self.Raw, nil) + return +} + +// Wrap bundles the message into an Envelope to transmit over the network. +// +// pow (Proof Of Work) controls how much time to spend on hashing the message, +// inherently controlling its priority through the network (smaller hash, bigger +// priority). +// +// The user can control the amount of identity, privacy and encryption through +// the options parameter as follows: +// - options.From == nil && options.To == nil: anonymous broadcast +// - options.From != nil && options.To == nil: signed broadcast (known sender) +// - options.From == nil && options.To != nil: encrypted anonymous message +// - options.From != nil && options.To != nil: encrypted signed message +func (self *SentMessage) Wrap(options Options) (envelope *Envelope, err error) { + if options.TTL == 0 { + options.TTL = DefaultTTL + } + self.appendPadding(options) + if options.Src != nil { + if err = self.sign(options.Src); err != nil { + return + } + } + if len(self.Raw) > msgMaxLength { + glog.V(logger.Error).Infof("Message size must not exceed %d bytes", msgMaxLength) + err = errors.New("Oversized message") + return + } + var salt, nonce []byte + if options.Dst != nil { + err = self.encryptAsymmetric(options.Dst) + } else if options.KeySym != nil { + salt, nonce, err = self.encryptSymmetric(options.KeySym) + } else { + err = errors.New("Unable to encrypt the message: neither Dst nor Key") + } + + if err == nil { + if (options.Topic == TopicType{}) { + options.Topic = DeriveTopicFromSymmetricKey(options.KeySym) + } + + envelope = NewEnvelope(options.TTL, options.Topic, salt, nonce, self) + envelope.Seal(options) + } + return +} + +// decryptSymmetric decrypts a message with a topic key, using AES-GCM-256. +// nonce size should be 12 bytes (see cipher.gcmStandardNonceSize). +func (self *ReceivedMessage) decryptSymmetric(key []byte, salt []byte, nonce []byte) error { + derivedKey := pbkdf2.Key(key, salt, kdfIterations, aesKeyLength, sha256.New) + + block, err := aes.NewCipher(derivedKey) + if err != nil { + return err + } + aesgcm, err := cipher.NewGCM(block) + if err != nil { + return err + } + if len(nonce) != aesgcm.NonceSize() { + glog.V(logger.Error).Infof("AES nonce size must be %d bytes", aesgcm.NonceSize()) + return errors.New("Wrong AES nonce size") + } + decrypted, err := aesgcm.Open(nil, nonce, self.Raw, nil) + if err != nil { + return err + } + self.Raw = decrypted + return nil +} + +// decryptAsymmetric decrypts an encrypted payload with a private key. +func (self *ReceivedMessage) decryptAsymmetric(key *ecdsa.PrivateKey) error { + decrypted, err := crypto.Decrypt(key, self.Raw) + if err == nil { + self.Raw = decrypted + } + return err +} + +// Validate checks the validity and extracts the fields in case of success +func (self *ReceivedMessage) Validate() bool { + sz := len(self.Raw) + cur := sz + if sz < 1 { + return false + } + + if isMessageSigned(self.Raw[0]) { + cur -= signatureLength + if cur <= 1 { + return false + } + self.Signature = self.Raw[cur:] + self.Src = self.Recover() + if self.Src == nil { + return false + } + } + + if isMessagePadded(self.Raw[0]) { + paddingSize := int(self.Raw[cur-1]) + beg := cur - paddingSize + if beg <= 1 { + return false + } + self.Padding = self.Raw[beg : cur-1] + cur = beg + } + + self.Payload = self.Raw[1:cur] + if self.isSymmetricEncryption() == self.isAsymmetricEncryption() { + return false + } + return true +} + +// Recover retrieves the public key of the message signer. +func (self *ReceivedMessage) Recover() *ecdsa.PublicKey { + defer func() { recover() }() // in case of invalid signature + + pub, err := crypto.SigToPub(self.hash(), self.Signature) + if err != nil { + glog.V(logger.Error).Infof("Could not get public key from signature: %v", err) + return nil + } + return pub +} + +// hash calculates the SHA3 checksum of the message flags, payload and padding. +func (self *ReceivedMessage) hash() []byte { + if isMessageSigned(self.Raw[0]) { + sz := len(self.Raw) - signatureLength + return crypto.Keccak256(self.Raw[:sz]) + } + return crypto.Keccak256(self.Raw) +} diff --git a/whisper/whisper05/peer.go b/whisper/whisper05/peer.go new file mode 100644 index 0000000000..a7851aa7cb --- /dev/null +++ b/whisper/whisper05/peer.go @@ -0,0 +1,175 @@ +// Copyright 2014 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package whisper05 + +import ( + "fmt" + "time" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/logger/glog" + "github.com/ethereum/go-ethereum/p2p" + "github.com/ethereum/go-ethereum/rlp" + set "gopkg.in/fatih/set.v0" +) + +// peer represents a whisper protocol peer connection. +type peer struct { + host *Whisper + peer *p2p.Peer + ws p2p.MsgReadWriter + + known *set.Set // Messages already known by the peer to avoid wasting bandwidth + + quit chan struct{} +} + +// newPeer creates a new whisper peer object, but does not run the handshake itself. +func newPeer(host *Whisper, remote *p2p.Peer, rw p2p.MsgReadWriter) *peer { + return &peer{ + host: host, + peer: remote, + ws: rw, + known: set.New(), + quit: make(chan struct{}), + } +} + +// start initiates the peer updater, periodically broadcasting the whisper packets +// into the network. +func (self *peer) start() { + go self.update() + glog.V(logger.Debug).Infof("%v: whisper started", self.peer) +} + +// stop terminates the peer updater, stopping message forwarding to it. +func (self *peer) stop() { + close(self.quit) + glog.V(logger.Debug).Infof("%v: whisper stopped", self.peer) +} + +// handshake sends the protocol initiation status message to the remote peer and +// verifies the remote status too. +func (self *peer) handshake() error { + // Send the handshake status message asynchronously + errc := make(chan error, 1) + go func() { + errc <- p2p.SendItems(self.ws, statusCode, protocolVersion) + }() + // Fetch the remote status packet and verify protocol match + packet, err := self.ws.ReadMsg() + if err != nil { + return err + } + if packet.Code != statusCode { + return fmt.Errorf("peer sent %x before status packet", packet.Code) + } + s := rlp.NewStream(packet.Payload, uint64(packet.Size)) + if _, err := s.List(); err != nil { + return fmt.Errorf("bad status message: %v", err) + } + peerVersion, err := s.Uint() + if err != nil { + return fmt.Errorf("bad status message: %v", err) + } + if peerVersion != protocolVersion { + return fmt.Errorf("protocol version mismatch %d != %d", peerVersion, protocolVersion) + } + // Wait until out own status is consumed too + if err := <-errc; err != nil { + return fmt.Errorf("failed to send status packet: %v", err) + } + return nil +} + +// update executes periodic operations on the peer, including message transmission +// and expiration. +func (self *peer) update() { + // Start the tickers for the updates + expire := time.NewTicker(expirationCycle) + transmit := time.NewTicker(transmissionCycle) + + // Loop and transmit until termination is requested + for { + select { + case <-expire.C: + self.expire() + + case <-transmit.C: + if err := self.broadcast(); err != nil { + glog.V(logger.Info).Infof("%v: broadcast failed: %v", self.peer, err) + return + } + + case <-self.quit: + return + } + } +} + +// mark marks an envelope known to the peer so that it won't be sent back. +func (self *peer) mark(envelope *Envelope) { + self.known.Add(envelope.Hash()) +} + +// marked checks if an envelope is already known to the remote peer. +func (self *peer) marked(envelope *Envelope) bool { + return self.known.Has(envelope.Hash()) +} + +// expire iterates over all the known envelopes in the host and removes all +// expired (unknown) ones from the known list. +func (self *peer) expire() { + // Assemble the list of available envelopes + available := set.NewNonTS() + for _, envelope := range self.host.Envelopes() { + available.Add(envelope.Hash()) + } + // Cross reference availability with known status + unmark := make(map[common.Hash]struct{}) + self.known.Each(func(v interface{}) bool { + if !available.Has(v.(common.Hash)) { + unmark[v.(common.Hash)] = struct{}{} + } + return true + }) + // Dump all known but unavailable + for hash, _ := range unmark { + self.known.Remove(hash) + } +} + +// broadcast iterates over the collection of envelopes and transmits yet unknown +// ones over the network. +func (self *peer) broadcast() error { + // Fetch the envelopes and collect the unknown ones + envelopes := self.host.Envelopes() + transmit := make([]*Envelope, 0, len(envelopes)) + for _, envelope := range envelopes { + if !self.marked(envelope) { + transmit = append(transmit, envelope) + self.mark(envelope) + } + } + // Transmit the unknown batch (potentially empty) + if err := p2p.Send(self.ws, messagesCode, transmit); err != nil { + return err + } + glog.V(logger.Detail).Infoln(self.peer, "broadcasted", len(transmit), "message(s)") + return nil +} diff --git a/whisper/whisper05/topic.go b/whisper/whisper05/topic.go new file mode 100644 index 0000000000..bcb7983515 --- /dev/null +++ b/whisper/whisper05/topic.go @@ -0,0 +1,92 @@ +// Copyright 2015 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +// Contains the Whisper protocol Topic element. For formal details please see +// the specs at https://github.com/ethereum/wiki/wiki/Whisper-PoC-1-Protocol-Spec#topics. + +package whisper05 + +import ( + "fmt" + "strings" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/crypto" +) + +// Topic represents a cryptographically secure, probabilistic partial +// classifications of a message, determined as the first (left) 4 bytes of the +// SHA3 hash of some arbitrary data given by the original author of the message. +type TopicType [topicLength]byte + +func BytesToTopic(b []byte) (t TopicType) { + sz := topicLength + if x := len(b); x < topicLength { + sz = x + } + for i := 0; i < sz; i++ { + t[i] = b[i] + } + return t +} + +func HashToTopic(h common.Hash) (t TopicType) { + for i := 0; i < topicLength; i++ { + t[i] = h[i] + } + return t +} + +func NewTopic(data []byte) TopicType { + if data == nil { + return TopicType{} + } + prefix := [topicLength]byte{} + copy(prefix[:], crypto.Keccak256(data)[:topicLength]) + return TopicType(prefix) +} + +// NewTopicFromString creates a topic using the binary data contents of the specified string. +func NewTopicFromString(data string) TopicType { + return NewTopic(common.FromHex(data)) +} + +// String converts a topic byte array to a string representation. +func (self *TopicType) String() string { + return string(common.ToHex(self[:])) +} + +// UnmarshalJSON parses a hex representation to a topic. +func (t *TopicType) UnmarshalJSON(input []byte) error { + length := len(input) + if length >= 2 && input[0] == '"' && input[length-1] == '"' { + input = input[1 : length-1] + } + // strip "0x" for length check + if len(input) > 1 && strings.ToLower(string(input[:2])) == "0x" { + input = input[2:] + } + // validate the length of the input + if len(input) != topicLength*2 { + return fmt.Errorf("whisper: unmarshalJSON failed: topic must be exactly %d bytes", topicLength) + } + b := common.FromHex(string(input)) + if b == nil { + return fmt.Errorf("whisper: unmarshalJSON failed: wrong topic format") + } + *t = BytesToTopic(b) + return nil +} diff --git a/whisper/whisper05/whisper.go b/whisper/whisper05/whisper.go new file mode 100644 index 0000000000..17fe18e93c --- /dev/null +++ b/whisper/whisper05/whisper.go @@ -0,0 +1,333 @@ +// Copyright 2014 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package whisper05 + +import ( + "crypto/ecdsa" + "sync" + "time" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/logger/glog" + "github.com/ethereum/go-ethereum/p2p" + "github.com/ethereum/go-ethereum/rpc" + + "fmt" + + set "gopkg.in/fatih/set.v0" +) + +// Whisper represents a dark communication interface through the Ethereum +// network, using its very own P2P communication layer. +type Whisper struct { + protocol p2p.Protocol + filters *Filters + + privateKeys map[string]*ecdsa.PrivateKey + //topicKeys map[TopicType][]byte // todo: move to the filter. this is not suitable because of possible collisions + + envelopes map[common.Hash]*Envelope // Pool of messages currently tracked by this node + messages map[common.Hash]*ReceivedMessage // Pool of successfully decrypted messages + expirations map[uint32]*set.SetNonTS // Message expiration pool (TODO: something lighter) + poolMu sync.RWMutex // Mutex to sync the message and expiration pools + + peers map[*peer]struct{} // Set of currently active peers + peerMu sync.RWMutex // Mutex to sync the active peer set + + quit chan struct{} +} + +// New creates a Whisper client ready to communicate through the Ethereum P2P network. +func New() *Whisper { + whisper := &Whisper{ + //filters: NewFilters(), + privateKeys: make(map[string]*ecdsa.PrivateKey), + //topicKeys: make(map[TopicType][]byte), + envelopes: make(map[common.Hash]*Envelope), + messages: make(map[common.Hash]*ReceivedMessage), + expirations: make(map[uint32]*set.SetNonTS), + peers: make(map[*peer]struct{}), + quit: make(chan struct{}), + } + whisper.filters = NewFilters(whisper) + whisper.filters.Start() + + // p2p whisper sub protocol handler + whisper.protocol = p2p.Protocol{ + Name: protocolName, + Version: uint(protocolVersion), + Length: 2, + Run: whisper.handlePeer, + } + + return whisper +} + +// APIs returns the RPC descriptors the Whisper implementation offers +func (s *Whisper) APIs() []rpc.API { + return []rpc.API{ + { + Namespace: protocolName, + Version: protocolVersionStr, + Service: NewPublicWhisperAPI(s), + Public: true, + }, + } +} + +// Protocols returns the whisper sub-protocols ran by this particular client. +func (self *Whisper) Protocols() []p2p.Protocol { + return []p2p.Protocol{self.protocol} +} + +// Version returns the whisper sub-protocols version number. +func (self *Whisper) Version() uint { + return self.protocol.Version +} + +// todo: review. maybe we need to delete these identity-related functions, since the key moved to Filter +// NewIdentity generates a new cryptographic identity for the client, and injects +// it into the known identities for message decryption. +func (self *Whisper) NewIdentity() *ecdsa.PrivateKey { + key, err := crypto.GenerateKey() + if err != nil { + panic(err) + } + if !validatePrivateKey(key) { + err = fmt.Errorf("Failed to generate valid key") + panic(err) + } + self.privateKeys[string(crypto.FromECDSAPub(&key.PublicKey))] = key + return key +} + +// HasIdentity checks if the the whisper node is configured with the private key +// of the specified public pair. +func (self *Whisper) HasIdentity(key *ecdsa.PublicKey) bool { + return self.privateKeys[string(crypto.FromECDSAPub(key))] != nil +} + +// GetIdentity retrieves the private key of the specified public identity. +func (self *Whisper) GetIdentity(key *ecdsa.PublicKey) *ecdsa.PrivateKey { + return self.privateKeys[string(crypto.FromECDSAPub(key))] +} + +// Watch installs a new message handler to run in case a matching packet arrives +// from the whisper network. +func (self *Whisper) Watch(f *Filter) int { + return self.filters.Install(f) +} + +// Unwatch removes an installed message handler. +func (self *Whisper) Unwatch(id int) { + self.filters.Uninstall(id) +} + +// Send injects a message into the whisper send queue, to be distributed in the +// network in the coming cycles. +func (self *Whisper) Send(envelope *Envelope) error { + return self.add(envelope) +} + +// Start implements node.Service, starting the background data propagation thread +// of the Whisper protocol. +func (self *Whisper) Start(*p2p.Server) error { + glog.V(logger.Info).Infoln("Whisper started") + go self.update() + return nil +} + +// Stop implements node.Service, stopping the background data propagation thread +// of the Whisper protocol. +func (self *Whisper) Stop() error { + close(self.quit) + glog.V(logger.Info).Infoln("Whisper stopped") + return nil +} + +// handlePeer is called by the underlying P2P layer when the whisper sub-protocol +// connection is negotiated. +func (self *Whisper) handlePeer(peer *p2p.Peer, rw p2p.MsgReadWriter) error { + // Create the new peer and start tracking it + whisperPeer := newPeer(self, peer, rw) + + self.peerMu.Lock() + self.peers[whisperPeer] = struct{}{} + self.peerMu.Unlock() + + defer func() { + self.peerMu.Lock() + delete(self.peers, whisperPeer) + self.peerMu.Unlock() + }() + + // Run the peer handshake and state updates + if err := whisperPeer.handshake(); err != nil { + return err + } + whisperPeer.start() + defer whisperPeer.stop() + + // Read and process inbound messages directly to merge into client-global state + for { + // Fetch the next packet and decode the contained envelopes + packet, err := rw.ReadMsg() + if err != nil { + return err + } + var envelopes []*Envelope + if err := packet.Decode(&envelopes); err != nil { + glog.V(logger.Info).Infof("%v: failed to decode envelope: %v", peer, err) + continue + } + // Inject all envelopes into the internal pool + for _, envelope := range envelopes { + if err := self.add(envelope); err != nil { + // TODO Punish peer here. Invalid envelope. + glog.V(logger.Debug).Infof("%v: failed to pool envelope: %v", peer, err) + } + whisperPeer.mark(envelope) + } + } +} + +// add inserts a new envelope into the message pool to be distributed within the +// whisper network. It also inserts the envelope into the expiration pool at the +// appropriate time-stamp. +func (self *Whisper) add(envelope *Envelope) error { + self.poolMu.Lock() + defer self.poolMu.Unlock() + + // short circuit when a received envelope has already expired + if envelope.Expiry < uint32(time.Now().Unix()) { + return nil + } + + // Insert the message into the tracked pool + hash := envelope.Hash() + if _, ok := self.envelopes[hash]; ok { + glog.V(logger.Detail).Infof("whisper envelope already cached: %x\n", envelope) + return nil + } + self.envelopes[hash] = envelope + + // Insert the message into the expiration pool for later removal + if self.expirations[envelope.Expiry] == nil { + self.expirations[envelope.Expiry] = set.NewNonTS() + } + if !self.expirations[envelope.Expiry].Has(hash) { + self.expirations[envelope.Expiry].Add(hash) + + // Notify the local node of a message arrival + go self.postEvent(envelope) + } + glog.V(logger.Detail).Infof("cached whisper envelope %x\n", envelope) + return nil +} + +// postEvent delivers the message to the watchers. +func (self *Whisper) postEvent(envelope *Envelope) { + self.filters.Notify(envelope) +} + +// update loops until the lifetime of the whisper node, updating its internal +// state by expiring stale messages from the pool. +func (self *Whisper) update() { + // Start a ticker to check for expirations + expire := time.NewTicker(expirationCycle) + + // Repeat updates until termination is requested + for { + select { + case <-expire.C: + self.expire() + + case <-self.quit: + return + } + } +} + +// expire iterates over all the expiration timestamps, removing all stale +// messages from the pools. +func (self *Whisper) expire() { + self.poolMu.Lock() + defer self.poolMu.Unlock() + + now := uint32(time.Now().Unix()) + for then, hashSet := range self.expirations { + // Short circuit if a future time + if then > now { + continue + } + // Dump all expired messages and remove timestamp + hashSet.Each(func(v interface{}) bool { + delete(self.envelopes, v.(common.Hash)) + delete(self.messages, v.(common.Hash)) + return true + }) + self.expirations[then].Clear() + } +} + +// envelopes retrieves all the messages currently pooled by the node. +func (self *Whisper) Envelopes() []*Envelope { + self.poolMu.RLock() + defer self.poolMu.RUnlock() + + all := make([]*Envelope, 0, len(self.envelopes)) + for _, envelope := range self.envelopes { + all = append(all, envelope) + } + return all +} + +// Messages retrieves all the currently pooled messages matching a filter id. +func (self *Whisper) Messages(id int) []*ReceivedMessage { + self.poolMu.RLock() + defer self.poolMu.RUnlock() + + result := make([]*ReceivedMessage, 0) + if filter := self.filters.Get(id); filter != nil { + for _, msg := range self.messages { + if filter.MatchMessage(msg) { + result = append(result, msg) + } + } + } + return result +} + +func (self *Whisper) addDecryptedMessage(msg *ReceivedMessage) { + self.poolMu.Lock() + defer self.poolMu.Unlock() + + self.messages[msg.EnvelopeHash] = msg +} + +func validatePublicKey(k *ecdsa.PublicKey) bool { + return k != nil && k.X != nil && k.Y != nil +} + +func validatePrivateKey(k *ecdsa.PrivateKey) bool { + if k == nil || k.D == nil { + return false + } + return validatePublicKey(&k.PublicKey) +}