2014-11-09 23:45:16 -08:00
|
|
|
package dht
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
2014-12-19 12:19:56 -08:00
|
|
|
"fmt"
|
2014-11-09 23:45:16 -08:00
|
|
|
"strings"
|
|
|
|
|
2014-11-11 16:42:37 -08:00
|
|
|
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
2014-12-19 12:19:56 -08:00
|
|
|
|
2014-12-29 05:45:55 -08:00
|
|
|
ci "github.com/jbenet/go-ipfs/p2p/crypto"
|
2014-12-29 05:43:56 -08:00
|
|
|
"github.com/jbenet/go-ipfs/p2p/peer"
|
2014-11-09 23:45:16 -08:00
|
|
|
pb "github.com/jbenet/go-ipfs/routing/dht/pb"
|
2015-01-17 02:50:10 +00:00
|
|
|
record "github.com/jbenet/go-ipfs/routing/record"
|
2014-11-09 23:45:16 -08:00
|
|
|
u "github.com/jbenet/go-ipfs/util"
|
2014-12-19 12:19:56 -08:00
|
|
|
ctxutil "github.com/jbenet/go-ipfs/util/ctx"
|
2014-11-09 23:45:16 -08:00
|
|
|
)
|
|
|
|
|
2014-11-11 19:43:53 -08:00
|
|
|
// ValidatorFunc is a function that is called to validate a given
|
|
|
|
// type of DHTRecord.
|
2014-11-09 23:45:16 -08:00
|
|
|
type ValidatorFunc func(u.Key, []byte) error
|
|
|
|
|
2014-11-11 19:43:53 -08:00
|
|
|
// ErrBadRecord is returned any time a dht record is found to be
|
|
|
|
// incorrectly formatted or signed.
|
2014-11-09 23:45:16 -08:00
|
|
|
var ErrBadRecord = errors.New("bad dht record")
|
2014-11-11 19:43:53 -08:00
|
|
|
|
|
|
|
// ErrInvalidRecordType is returned if a DHTRecord keys prefix
|
|
|
|
// is not found in the Validator map of the DHT.
|
2014-11-09 23:45:16 -08:00
|
|
|
var ErrInvalidRecordType = errors.New("invalid record keytype")
|
|
|
|
|
2014-12-19 12:19:56 -08:00
|
|
|
// KeyForPublicKey returns the key used to retrieve public keys
|
|
|
|
// from the dht.
|
|
|
|
func KeyForPublicKey(id peer.ID) u.Key {
|
|
|
|
return u.Key("/pk/" + string(id))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dht *IpfsDHT) getPublicKeyOnline(ctx context.Context, p peer.ID) (ci.PubKey, error) {
|
|
|
|
log.Debugf("getPublicKey for: %s", p)
|
|
|
|
|
|
|
|
// check locally.
|
|
|
|
pk := dht.peerstore.PubKey(p)
|
|
|
|
if pk != nil {
|
|
|
|
return pk, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ok, try the node itself. if they're overwhelmed or slow we can move on.
|
|
|
|
ctxT, _ := ctxutil.WithDeadlineFraction(ctx, 0.3)
|
|
|
|
if pk, err := dht.getPublicKeyFromNode(ctx, p); err == nil {
|
|
|
|
return pk, nil
|
2014-11-11 16:28:20 -08:00
|
|
|
}
|
|
|
|
|
2014-12-19 12:19:56 -08:00
|
|
|
// last ditch effort: let's try the dht.
|
|
|
|
log.Debugf("pk for %s not in peerstore, and peer failed. trying dht.", p)
|
|
|
|
pkkey := KeyForPublicKey(p)
|
|
|
|
|
|
|
|
// ok, try the node itself. if they're overwhelmed or slow we can move on.
|
|
|
|
val, err := dht.GetValue(ctxT, pkkey)
|
2014-11-11 16:28:20 -08:00
|
|
|
if err != nil {
|
|
|
|
log.Warning("Failed to find requested public key.")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-12-19 12:19:56 -08:00
|
|
|
pk, err = ci.UnmarshalPublicKey(val)
|
2014-11-11 16:28:20 -08:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to unmarshal public key: %s", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-12-19 12:19:56 -08:00
|
|
|
return pk, nil
|
2014-11-11 16:28:20 -08:00
|
|
|
}
|
|
|
|
|
2014-12-19 12:19:56 -08:00
|
|
|
func (dht *IpfsDHT) getPublicKeyFromNode(ctx context.Context, p peer.ID) (ci.PubKey, error) {
|
|
|
|
|
|
|
|
// check locally, just in case...
|
|
|
|
pk := dht.peerstore.PubKey(p)
|
|
|
|
if pk != nil {
|
|
|
|
return pk, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pkkey := KeyForPublicKey(p)
|
|
|
|
pmes, err := dht.getValueSingle(ctx, p, pkkey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// node doesn't have key :(
|
|
|
|
record := pmes.GetRecord()
|
|
|
|
if record == nil {
|
|
|
|
return nil, fmt.Errorf("node not responding with its public key: %s", p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Success! We were given the value. we don't need to check
|
|
|
|
// validity because a) we can't. b) we know the hash of the
|
|
|
|
// key we're looking for.
|
|
|
|
val := record.GetValue()
|
|
|
|
log.Debug("dht got a value from other peer.")
|
|
|
|
|
|
|
|
pk, err = ci.UnmarshalPublicKey(val)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := peer.IDFromPublicKey(pk)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if id != p {
|
|
|
|
return nil, fmt.Errorf("public key does not match id: %s", p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ok! it's valid. we got it!
|
|
|
|
log.Debugf("dht got public key from node itself.")
|
|
|
|
return pk, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// verifyRecordLocally attempts to verify a record. if we do not have the public
|
|
|
|
// key, we fail. we do not search the dht.
|
|
|
|
func (dht *IpfsDHT) verifyRecordLocally(r *pb.Record) error {
|
|
|
|
|
2014-11-09 23:45:16 -08:00
|
|
|
// First, validate the signature
|
2014-12-19 12:19:56 -08:00
|
|
|
p := peer.ID(r.GetAuthor())
|
|
|
|
pk := dht.peerstore.PubKey(p)
|
|
|
|
if pk == nil {
|
|
|
|
return fmt.Errorf("do not have public key for %s", p)
|
|
|
|
}
|
|
|
|
|
|
|
|
return dht.verifyRecord(r, pk)
|
|
|
|
}
|
|
|
|
|
|
|
|
// verifyRecordOnline verifies a record, searching the DHT for the public key
|
|
|
|
// if necessary. The reason there is a distinction in the functions is that
|
|
|
|
// retrieving arbitrary public keys from the DHT as a result of passively
|
|
|
|
// receiving records (e.g. through a PUT_VALUE or ADD_PROVIDER) can cause a
|
|
|
|
// massive amplification attack on the dht. Use with care.
|
|
|
|
func (dht *IpfsDHT) verifyRecordOnline(ctx context.Context, r *pb.Record) error {
|
|
|
|
|
|
|
|
// get the public key, search for it if necessary.
|
|
|
|
p := peer.ID(r.GetAuthor())
|
|
|
|
pk, err := dht.getPublicKeyOnline(ctx, p)
|
2014-11-09 23:45:16 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-12-19 12:19:56 -08:00
|
|
|
return dht.verifyRecord(r, pk)
|
|
|
|
}
|
2014-11-09 23:45:16 -08:00
|
|
|
|
2015-01-16 23:53:56 +00:00
|
|
|
// TODO: make this an independent exported function.
|
|
|
|
// it might be useful for users to have access to.
|
2014-12-19 12:19:56 -08:00
|
|
|
func (dht *IpfsDHT) verifyRecord(r *pb.Record, pk ci.PubKey) error {
|
|
|
|
// First, validate the signature
|
2015-01-17 02:50:10 +00:00
|
|
|
blob := record.RecordBlobForSig(r)
|
2014-12-19 12:19:56 -08:00
|
|
|
ok, err := pk.Verify(blob, r.GetSignature())
|
2014-11-09 23:45:16 -08:00
|
|
|
if err != nil {
|
2014-11-10 14:22:56 -08:00
|
|
|
log.Error("Signature verify failed.")
|
2014-11-09 23:45:16 -08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !ok {
|
2014-12-19 12:19:56 -08:00
|
|
|
log.Error("dht found a forged record! (ignored)")
|
2014-11-09 23:45:16 -08:00
|
|
|
return ErrBadRecord
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now, check validity func
|
|
|
|
parts := strings.Split(r.GetKey(), "/")
|
2014-11-10 14:22:56 -08:00
|
|
|
if len(parts) < 3 {
|
2015-01-05 04:48:50 -08:00
|
|
|
log.Infof("Record key does not have validator: %s", u.Key(r.GetKey()))
|
|
|
|
return nil
|
2014-11-09 23:45:16 -08:00
|
|
|
}
|
|
|
|
|
2014-11-10 14:22:56 -08:00
|
|
|
fnc, ok := dht.Validators[parts[1]]
|
2014-11-09 23:45:16 -08:00
|
|
|
if !ok {
|
2014-11-10 14:22:56 -08:00
|
|
|
log.Errorf("Unrecognized key prefix: %s", parts[1])
|
2014-11-09 23:45:16 -08:00
|
|
|
return ErrInvalidRecordType
|
|
|
|
}
|
|
|
|
|
|
|
|
return fnc(u.Key(r.GetKey()), r.GetValue())
|
|
|
|
}
|
2014-11-10 15:48:49 -08:00
|
|
|
|
2014-11-11 19:43:53 -08:00
|
|
|
// ValidatePublicKeyRecord implements ValidatorFunc and
|
|
|
|
// verifies that the passed in record value is the PublicKey
|
|
|
|
// that matches the passed in key.
|
2014-11-10 15:48:49 -08:00
|
|
|
func ValidatePublicKeyRecord(k u.Key, val []byte) error {
|
2014-11-11 16:28:20 -08:00
|
|
|
keyparts := bytes.Split([]byte(k), []byte("/"))
|
|
|
|
if len(keyparts) < 3 {
|
|
|
|
return errors.New("invalid key")
|
|
|
|
}
|
|
|
|
|
|
|
|
pkh := u.Hash(val)
|
|
|
|
if !bytes.Equal(keyparts[2], pkh) {
|
|
|
|
return errors.New("public key does not match storage key")
|
|
|
|
}
|
2014-11-10 15:48:49 -08:00
|
|
|
return nil
|
|
|
|
}
|