2020-04-08 09:57:23 +02:00
|
|
|
// Copyright 2020 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
2020-10-14 12:28:17 +02:00
|
|
|
package v5wire
|
2020-04-08 09:57:23 +02:00
|
|
|
|
|
|
|
import (
|
2020-10-14 12:28:17 +02:00
|
|
|
"crypto/ecdsa"
|
2020-04-08 09:57:23 +02:00
|
|
|
crand "crypto/rand"
|
2020-10-14 12:28:17 +02:00
|
|
|
"encoding/binary"
|
|
|
|
"time"
|
2020-04-08 09:57:23 +02:00
|
|
|
|
2022-11-14 15:41:56 +01:00
|
|
|
"github.com/ethereum/go-ethereum/common/lru"
|
2020-04-08 09:57:23 +02:00
|
|
|
"github.com/ethereum/go-ethereum/common/mclock"
|
2020-10-14 12:28:17 +02:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2020-04-08 09:57:23 +02:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
|
|
|
)
|
|
|
|
|
2020-10-14 12:28:17 +02:00
|
|
|
const handshakeTimeout = time.Second
|
|
|
|
|
|
|
|
// The SessionCache keeps negotiated encryption keys and
|
2020-04-08 09:57:23 +02:00
|
|
|
// state for in-progress handshakes in the Discovery v5 wire protocol.
|
2020-10-14 12:28:17 +02:00
|
|
|
type SessionCache struct {
|
2022-11-14 15:41:56 +01:00
|
|
|
sessions lru.BasicLRU[sessionID, *session]
|
2020-10-14 12:28:17 +02:00
|
|
|
handshakes map[sessionID]*Whoareyou
|
2020-04-08 09:57:23 +02:00
|
|
|
clock mclock.Clock
|
2020-10-14 12:28:17 +02:00
|
|
|
|
|
|
|
// hooks for overriding randomness.
|
|
|
|
nonceGen func(uint32) (Nonce, error)
|
|
|
|
maskingIVGen func([]byte) error
|
|
|
|
ephemeralKeyGen func() (*ecdsa.PrivateKey, error)
|
2020-04-08 09:57:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// sessionID identifies a session or handshake.
|
|
|
|
type sessionID struct {
|
|
|
|
id enode.ID
|
|
|
|
addr string
|
|
|
|
}
|
|
|
|
|
|
|
|
// session contains session information
|
|
|
|
type session struct {
|
|
|
|
writeKey []byte
|
|
|
|
readKey []byte
|
|
|
|
nonceCounter uint32
|
|
|
|
}
|
|
|
|
|
2020-10-14 12:28:17 +02:00
|
|
|
// keysFlipped returns a copy of s with the read and write keys flipped.
|
|
|
|
func (s *session) keysFlipped() *session {
|
|
|
|
return &session{s.readKey, s.writeKey, s.nonceCounter}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSessionCache(maxItems int, clock mclock.Clock) *SessionCache {
|
|
|
|
return &SessionCache{
|
2022-11-14 15:41:56 +01:00
|
|
|
sessions: lru.NewBasicLRU[sessionID, *session](maxItems),
|
2020-10-14 12:28:17 +02:00
|
|
|
handshakes: make(map[sessionID]*Whoareyou),
|
|
|
|
clock: clock,
|
|
|
|
nonceGen: generateNonce,
|
|
|
|
maskingIVGen: generateMaskingIV,
|
|
|
|
ephemeralKeyGen: crypto.GenerateKey,
|
2020-04-08 09:57:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-14 12:28:17 +02:00
|
|
|
func generateNonce(counter uint32) (n Nonce, err error) {
|
|
|
|
binary.BigEndian.PutUint32(n[:4], counter)
|
|
|
|
_, err = crand.Read(n[4:])
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateMaskingIV(buf []byte) error {
|
|
|
|
_, err := crand.Read(buf)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-08 09:57:23 +02:00
|
|
|
// nextNonce creates a nonce for encrypting a message to the given session.
|
2020-10-14 12:28:17 +02:00
|
|
|
func (sc *SessionCache) nextNonce(s *session) (Nonce, error) {
|
|
|
|
s.nonceCounter++
|
|
|
|
return sc.nonceGen(s.nonceCounter)
|
2020-04-08 09:57:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// session returns the current session for the given node, if any.
|
2020-10-14 12:28:17 +02:00
|
|
|
func (sc *SessionCache) session(id enode.ID, addr string) *session {
|
2022-11-14 15:41:56 +01:00
|
|
|
item, _ := sc.sessions.Get(sessionID{id, addr})
|
|
|
|
return item
|
2020-04-08 09:57:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// readKey returns the current read key for the given node.
|
2020-10-14 12:28:17 +02:00
|
|
|
func (sc *SessionCache) readKey(id enode.ID, addr string) []byte {
|
2020-04-08 09:57:23 +02:00
|
|
|
if s := sc.session(id, addr); s != nil {
|
|
|
|
return s.readKey
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// storeNewSession stores new encryption keys in the cache.
|
2020-10-14 12:28:17 +02:00
|
|
|
func (sc *SessionCache) storeNewSession(id enode.ID, addr string, s *session) {
|
|
|
|
sc.sessions.Add(sessionID{id, addr}, s)
|
2020-04-08 09:57:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// getHandshake gets the handshake challenge we previously sent to the given remote node.
|
2020-10-14 12:28:17 +02:00
|
|
|
func (sc *SessionCache) getHandshake(id enode.ID, addr string) *Whoareyou {
|
2020-04-08 09:57:23 +02:00
|
|
|
return sc.handshakes[sessionID{id, addr}]
|
|
|
|
}
|
|
|
|
|
|
|
|
// storeSentHandshake stores the handshake challenge sent to the given remote node.
|
2020-10-14 12:28:17 +02:00
|
|
|
func (sc *SessionCache) storeSentHandshake(id enode.ID, addr string, challenge *Whoareyou) {
|
2020-04-08 09:57:23 +02:00
|
|
|
challenge.sent = sc.clock.Now()
|
|
|
|
sc.handshakes[sessionID{id, addr}] = challenge
|
|
|
|
}
|
|
|
|
|
|
|
|
// deleteHandshake deletes handshake data for the given node.
|
2020-10-14 12:28:17 +02:00
|
|
|
func (sc *SessionCache) deleteHandshake(id enode.ID, addr string) {
|
2020-04-08 09:57:23 +02:00
|
|
|
delete(sc.handshakes, sessionID{id, addr})
|
|
|
|
}
|
|
|
|
|
|
|
|
// handshakeGC deletes timed-out handshakes.
|
2020-10-14 12:28:17 +02:00
|
|
|
func (sc *SessionCache) handshakeGC() {
|
2020-04-08 09:57:23 +02:00
|
|
|
deadline := sc.clock.Now().Add(-handshakeTimeout)
|
|
|
|
for key, challenge := range sc.handshakes {
|
|
|
|
if challenge.sent < deadline {
|
|
|
|
delete(sc.handshakes, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|