records.go 3.18 KB
Newer Older
1 2 3 4 5 6
package dht

import (
	"bytes"
	"errors"
	"strings"
Jeromy's avatar
Jeromy committed
7
	"time"
8

Jeromy's avatar
Jeromy committed
9 10
	"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"
Jeromy's avatar
Jeromy committed
11
	ci "github.com/jbenet/go-ipfs/crypto"
12 13 14 15 16
	"github.com/jbenet/go-ipfs/peer"
	pb "github.com/jbenet/go-ipfs/routing/dht/pb"
	u "github.com/jbenet/go-ipfs/util"
)

Jeromy's avatar
Jeromy committed
17 18
// ValidatorFunc is a function that is called to validate a given
// type of DHTRecord.
19 20
type ValidatorFunc func(u.Key, []byte) error

Jeromy's avatar
Jeromy committed
21 22
// ErrBadRecord is returned any time a dht record is found to be
// incorrectly formatted or signed.
23
var ErrBadRecord = errors.New("bad dht record")
Jeromy's avatar
Jeromy committed
24 25 26

// ErrInvalidRecordType is returned if a DHTRecord keys prefix
// is not found in the Validator map of the DHT.
27 28 29 30 31 32
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)

Jeromy's avatar
Jeromy committed
33
	record.Key = proto.String(string(key))
34 35 36 37 38 39 40 41 42 43 44
	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
}

Jeromy's avatar
Jeromy committed
45 46
func (dht *IpfsDHT) getPublicKey(pid peer.ID) (ci.PubKey, error) {
	log.Debug("getPublicKey for: %s", pid)
47
	p, err := dht.peerstore.FindOrCreate(pid)
Jeromy's avatar
Jeromy committed
48 49 50 51 52
	if err == nil {
		return p.PubKey(), nil
	}

	log.Debug("not in peerstore, searching dht.")
53
	ctxT, _ := context.WithTimeout(dht.ContextGroup.Context(), time.Second*5)
Jeromy's avatar
Jeromy committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67
	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
}

68 69
func (dht *IpfsDHT) verifyRecord(r *pb.Record) error {
	// First, validate the signature
70
	p, err := dht.peerstore.FindOrCreate(peer.ID(r.GetAuthor()))
71 72 73
	if err != nil {
		return err
	}
Jeromy's avatar
Jeromy committed
74
	k := u.Key(r.GetKey())
75

Jeromy's avatar
Jeromy committed
76
	blob := bytes.Join([][]byte{[]byte(k),
77
		r.GetValue(),
Jeromy's avatar
Jeromy committed
78
		[]byte(r.GetAuthor())}, []byte{})
79 80 81

	ok, err := p.PubKey().Verify(blob, r.GetSignature())
	if err != nil {
Jeromy's avatar
Jeromy committed
82
		log.Error("Signature verify failed.")
83 84 85 86 87 88 89 90 91
		return err
	}

	if !ok {
		return ErrBadRecord
	}

	// Now, check validity func
	parts := strings.Split(r.GetKey(), "/")
Jeromy's avatar
Jeromy committed
92 93
	if len(parts) < 3 {
		log.Errorf("Record had bad key: %s", u.Key(r.GetKey()))
94 95 96
		return ErrBadRecord
	}

Jeromy's avatar
Jeromy committed
97
	fnc, ok := dht.Validators[parts[1]]
98
	if !ok {
Jeromy's avatar
Jeromy committed
99
		log.Errorf("Unrecognized key prefix: %s", parts[1])
100 101 102 103 104
		return ErrInvalidRecordType
	}

	return fnc(u.Key(r.GetKey()), r.GetValue())
}
105

Jeromy's avatar
Jeromy committed
106 107 108
// ValidatePublicKeyRecord implements ValidatorFunc and
// verifies that the passed in record value is the PublicKey
// that matches the passed in key.
109
func ValidatePublicKeyRecord(k u.Key, val []byte) error {
Jeromy's avatar
Jeromy committed
110 111 112 113 114 115 116 117 118
	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")
	}
119 120
	return nil
}