go-libp2p-kad-dht/records.go

121 lines
3.2 KiB
Go
Raw Normal View History

package dht
import (
"bytes"
"errors"
"strings"
2014-11-11 16:28:20 -08:00
"time"
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"
"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.
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.
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.
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))
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)
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
}
func (dht *IpfsDHT) verifyRecord(r *pb.Record) error {
// First, validate the signature
p, err := dht.peerstore.FindOrCreate(peer.ID(r.GetAuthor()))
if err != nil {
return err
}
2014-11-10 14:22:56 -08:00
k := u.Key(r.GetKey())
2014-11-10 14:22:56 -08:00
blob := bytes.Join([][]byte{[]byte(k),
r.GetValue(),
2014-11-10 14:22:56 -08:00
[]byte(r.GetAuthor())}, []byte{})
ok, err := p.PubKey().Verify(blob, r.GetSignature())
if err != nil {
2014-11-10 14:22:56 -08:00
log.Error("Signature verify failed.")
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()))
return ErrBadRecord
}
2014-11-10 14:22:56 -08:00
fnc, ok := dht.Validators[parts[1]]
if !ok {
2014-11-10 14:22:56 -08:00
log.Errorf("Unrecognized key prefix: %s", parts[1])
return ErrInvalidRecordType
}
return fnc(u.Key(r.GetKey()), r.GetValue())
}
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.
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")
}
return nil
}