2014-11-09 23:45:16 -08:00
|
|
|
package dht
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"strings"
|
2014-11-11 16:28:20 -08:00
|
|
|
"time"
|
2014-11-09 23:45:16 -08:00
|
|
|
|
2014-11-11 16:42:37 -08:00
|
|
|
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
|
|
|
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
|
2014-11-11 16:28:20 -08:00
|
|
|
ci "github.com/jbenet/go-ipfs/crypto"
|
2014-11-09 23:45:16 -08:00
|
|
|
"github.com/jbenet/go-ipfs/peer"
|
|
|
|
pb "github.com/jbenet/go-ipfs/routing/dht/pb"
|
|
|
|
u "github.com/jbenet/go-ipfs/util"
|
|
|
|
)
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
// creates and signs a dht record for the given key/value pair
|
|
|
|
func (dht *IpfsDHT) makePutRecord(key u.Key, value []byte) (*pb.Record, error) {
|
|
|
|
record := new(pb.Record)
|
|
|
|
|
2014-11-10 14:22:56 -08:00
|
|
|
record.Key = proto.String(string(key))
|
2014-11-09 23:45:16 -08:00
|
|
|
record.Value = value
|
|
|
|
record.Author = proto.String(string(dht.self.ID()))
|
|
|
|
blob := bytes.Join([][]byte{[]byte(key), value, []byte(dht.self.ID())}, []byte{})
|
|
|
|
sig, err := dht.self.PrivKey().Sign(blob)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
record.Signature = sig
|
|
|
|
return record, nil
|
|
|
|
}
|
|
|
|
|
2014-11-11 16:28:20 -08:00
|
|
|
func (dht *IpfsDHT) getPublicKey(pid peer.ID) (ci.PubKey, error) {
|
|
|
|
log.Debug("getPublicKey for: %s", pid)
|
2014-12-08 01:09:37 -08:00
|
|
|
p, err := dht.peerstore.FindOrCreate(pid)
|
2014-11-11 16:28:20 -08:00
|
|
|
if err == nil {
|
|
|
|
return p.PubKey(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug("not in peerstore, searching dht.")
|
2014-12-16 08:55:46 -08:00
|
|
|
ctxT, _ := context.WithTimeout(dht.ContextGroup.Context(), time.Second*5)
|
2014-11-11 16:28:20 -08:00
|
|
|
val, err := dht.GetValue(ctxT, u.Key("/pk/"+string(pid)))
|
|
|
|
if err != nil {
|
|
|
|
log.Warning("Failed to find requested public key.")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pubkey, err := ci.UnmarshalPublicKey(val)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to unmarshal public key: %s", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return pubkey, nil
|
|
|
|
}
|
|
|
|
|
2014-11-09 23:45:16 -08:00
|
|
|
func (dht *IpfsDHT) verifyRecord(r *pb.Record) error {
|
|
|
|
// First, validate the signature
|
2014-12-08 01:09:37 -08:00
|
|
|
p, err := dht.peerstore.FindOrCreate(peer.ID(r.GetAuthor()))
|
2014-11-09 23:45:16 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-11-10 14:22:56 -08:00
|
|
|
k := u.Key(r.GetKey())
|
2014-11-09 23:45:16 -08:00
|
|
|
|
2014-11-10 14:22:56 -08:00
|
|
|
blob := bytes.Join([][]byte{[]byte(k),
|
2014-11-09 23:45:16 -08:00
|
|
|
r.GetValue(),
|
2014-11-10 14:22:56 -08:00
|
|
|
[]byte(r.GetAuthor())}, []byte{})
|
2014-11-09 23:45:16 -08:00
|
|
|
|
|
|
|
ok, err := p.PubKey().Verify(blob, r.GetSignature())
|
|
|
|
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 {
|
|
|
|
return ErrBadRecord
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now, check validity func
|
|
|
|
parts := strings.Split(r.GetKey(), "/")
|
2014-11-10 14:22:56 -08:00
|
|
|
if len(parts) < 3 {
|
|
|
|
log.Errorf("Record had bad key: %s", u.Key(r.GetKey()))
|
2014-11-09 23:45:16 -08:00
|
|
|
return ErrBadRecord
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|