2015-07-06 19:54:22 -05:00
|
|
|
// Copyright 2014 The go-ethereum Authors
|
2015-07-22 11:48:40 -05:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-06 19:54:22 -05:00
|
|
|
//
|
2015-07-23 11:35:11 -05:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-06 19:54:22 -05:00
|
|
|
// 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.
|
|
|
|
//
|
2015-07-22 11:48:40 -05:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-06 19:54:22 -05:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 11:48:40 -05:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-06 19:54:22 -05:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 11:48:40 -05:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-06 19:54:22 -05:00
|
|
|
|
2015-04-10 07:53:21 -05:00
|
|
|
// 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.
|
|
|
|
|
2016-10-29 07:11:37 -05:00
|
|
|
package whisperv2
|
2014-12-08 05:43:33 -06:00
|
|
|
|
2014-12-09 17:03:50 -06:00
|
|
|
import (
|
2014-12-10 07:17:32 -06:00
|
|
|
"crypto/ecdsa"
|
2017-02-18 02:24:12 -06:00
|
|
|
crand "crypto/rand"
|
2017-02-22 06:10:07 -06:00
|
|
|
"fmt"
|
2015-04-10 07:53:21 -05:00
|
|
|
"math/rand"
|
2014-12-09 17:03:50 -06:00
|
|
|
"time"
|
|
|
|
|
2015-04-20 06:56:38 -05:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2014-12-09 17:03:50 -06:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2017-02-18 02:24:12 -06:00
|
|
|
"github.com/ethereum/go-ethereum/crypto/ecies"
|
2017-02-22 06:10:07 -06:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2014-12-09 17:03:50 -06:00
|
|
|
)
|
|
|
|
|
2015-04-20 06:56:38 -05:00
|
|
|
// Message represents an end-user data packet to transmit through the Whisper
|
2015-04-10 07:53:21 -05:00
|
|
|
// protocol. These are wrapped into Envelopes that need not be understood by
|
|
|
|
// intermediate nodes, just forwarded.
|
2014-12-08 05:43:33 -06:00
|
|
|
type Message struct {
|
2015-04-10 08:52:01 -05:00
|
|
|
Flags byte // First bit is signature presence, rest reserved and should be random
|
2014-12-08 05:43:33 -06:00
|
|
|
Signature []byte
|
|
|
|
Payload []byte
|
2015-04-21 03:43:11 -05:00
|
|
|
|
|
|
|
Sent time.Time // Time when the message was posted into the network
|
|
|
|
TTL time.Duration // Maximum time to live allowed for the message
|
2015-02-10 06:20:06 -06:00
|
|
|
|
2015-04-20 06:56:38 -05:00
|
|
|
To *ecdsa.PublicKey // Message recipient (identity used to decode the message)
|
2015-04-21 03:43:11 -05:00
|
|
|
Hash common.Hash // Message envelope hash to act as a unique id
|
2014-12-08 05:43:33 -06:00
|
|
|
}
|
|
|
|
|
2015-04-10 07:53:21 -05:00
|
|
|
// Options specifies the exact way a message should be wrapped into an Envelope.
|
|
|
|
type Options struct {
|
|
|
|
From *ecdsa.PrivateKey
|
|
|
|
To *ecdsa.PublicKey
|
|
|
|
TTL time.Duration
|
2015-04-14 03:12:09 -05:00
|
|
|
Topics []Topic
|
2015-04-10 07:53:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewMessage creates and initializes a non-signed, non-encrypted Whisper message.
|
2014-12-08 05:43:33 -06:00
|
|
|
func NewMessage(payload []byte) *Message {
|
2015-04-13 03:31:51 -05:00
|
|
|
// Construct an initial flag set: no signature, rest random
|
|
|
|
flags := byte(rand.Intn(256))
|
|
|
|
flags &= ^signatureFlag
|
2015-04-10 07:53:21 -05:00
|
|
|
|
|
|
|
// Assemble and return the message
|
|
|
|
return &Message{
|
|
|
|
Flags: flags,
|
|
|
|
Payload: payload,
|
2015-04-21 03:43:11 -05:00
|
|
|
Sent: time.Now(),
|
2015-04-10 07:53:21 -05:00
|
|
|
}
|
2014-12-08 05:43:33 -06:00
|
|
|
}
|
|
|
|
|
2015-04-10 07:53:21 -05:00
|
|
|
// Wrap bundles the message into an Envelope to transmit over the network.
|
|
|
|
//
|
2015-04-12 06:34:53 -05:00
|
|
|
// pow (Proof Of Work) controls how much time to spend on hashing the message,
|
2015-04-10 07:53:21 -05:00
|
|
|
// 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 *Message) Wrap(pow time.Duration, options Options) (*Envelope, error) {
|
|
|
|
// Use the default TTL if non was specified
|
|
|
|
if options.TTL == 0 {
|
2015-04-14 07:16:02 -05:00
|
|
|
options.TTL = DefaultTTL
|
2015-04-10 07:53:21 -05:00
|
|
|
}
|
2015-04-21 03:43:11 -05:00
|
|
|
self.TTL = options.TTL
|
|
|
|
|
2015-04-10 07:53:21 -05:00
|
|
|
// Sign and encrypt the message if requested
|
|
|
|
if options.From != nil {
|
|
|
|
if err := self.sign(options.From); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if options.To != nil {
|
|
|
|
if err := self.encrypt(options.To); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Wrap the processed message, seal it and return
|
2015-04-14 03:12:09 -05:00
|
|
|
envelope := NewEnvelope(options.TTL, options.Topics, self)
|
2015-04-10 07:53:21 -05:00
|
|
|
envelope.Seal(pow)
|
|
|
|
|
|
|
|
return envelope, nil
|
2014-12-09 17:03:50 -06:00
|
|
|
}
|
|
|
|
|
2015-04-12 06:34:53 -05:00
|
|
|
// sign calculates and sets the cryptographic signature for the message , also
|
2015-04-10 07:53:21 -05:00
|
|
|
// setting the sign flag.
|
2014-12-10 07:17:32 -06:00
|
|
|
func (self *Message) sign(key *ecdsa.PrivateKey) (err error) {
|
2015-04-13 03:31:51 -05:00
|
|
|
self.Flags |= signatureFlag
|
2014-12-09 17:03:50 -06:00
|
|
|
self.Signature, err = crypto.Sign(self.hash(), key)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-10 07:53:21 -05:00
|
|
|
// Recover retrieves the public key of the message signer.
|
2014-12-10 07:17:32 -06:00
|
|
|
func (self *Message) Recover() *ecdsa.PublicKey {
|
2015-04-10 07:53:21 -05:00
|
|
|
defer func() { recover() }() // in case of invalid signature
|
|
|
|
|
2015-04-16 10:24:39 -05:00
|
|
|
// Short circuit if no signature is present
|
|
|
|
if self.Signature == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Otherwise try and recover the signature
|
2015-04-05 12:31:18 -05:00
|
|
|
pub, err := crypto.SigToPub(self.hash(), self.Signature)
|
|
|
|
if err != nil {
|
2017-02-22 06:10:07 -06:00
|
|
|
log.Error(fmt.Sprintf("Could not get public key from signature: %v", err))
|
2015-04-05 12:31:18 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return pub
|
2014-12-10 07:17:32 -06:00
|
|
|
}
|
|
|
|
|
2015-04-12 06:34:53 -05:00
|
|
|
// encrypt encrypts a message payload with a public key.
|
2015-04-13 03:31:51 -05:00
|
|
|
func (self *Message) encrypt(key *ecdsa.PublicKey) (err error) {
|
2017-02-18 02:24:12 -06:00
|
|
|
self.Payload, err = ecies.Encrypt(crand.Reader, ecies.ImportECDSAPublic(key), self.Payload, nil, nil)
|
2015-04-13 03:31:51 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// decrypt decrypts an encrypted payload with a private key.
|
2015-04-21 04:13:57 -05:00
|
|
|
func (self *Message) decrypt(key *ecdsa.PrivateKey) error {
|
2017-02-18 02:24:12 -06:00
|
|
|
cleartext, err := ecies.ImportECDSA(key).Decrypt(crand.Reader, self.Payload, nil, nil)
|
2015-04-21 04:13:57 -05:00
|
|
|
if err == nil {
|
|
|
|
self.Payload = cleartext
|
|
|
|
}
|
|
|
|
return err
|
2014-12-08 05:43:33 -06:00
|
|
|
}
|
2014-12-09 17:03:50 -06:00
|
|
|
|
2015-04-12 06:34:53 -05:00
|
|
|
// hash calculates the SHA3 checksum of the message flags and payload.
|
2015-04-10 07:53:21 -05:00
|
|
|
func (self *Message) hash() []byte {
|
2016-02-21 12:40:27 -06:00
|
|
|
return crypto.Keccak256(append([]byte{self.Flags}, self.Payload...))
|
2014-12-09 17:03:50 -06:00
|
|
|
}
|
|
|
|
|
2015-04-12 06:34:53 -05:00
|
|
|
// bytes flattens the message contents (flags, signature and payload) into a
|
2015-04-10 07:53:21 -05:00
|
|
|
// single binary blob.
|
|
|
|
func (self *Message) bytes() []byte {
|
|
|
|
return append([]byte{self.Flags}, append(self.Signature, self.Payload...)...)
|
2014-12-09 17:03:50 -06:00
|
|
|
}
|