validation.go 2.59 KB
Newer Older
1 2 3 4 5 6
package record

import (
	"bytes"
	"errors"

7
	key "github.com/ipfs/go-ipfs/blocks/key"
8
	ci "github.com/ipfs/go-ipfs/p2p/crypto"
rht's avatar
rht committed
9
	path "github.com/ipfs/go-ipfs/path"
10 11
	pb "github.com/ipfs/go-ipfs/routing/dht/pb"
	u "github.com/ipfs/go-ipfs/util"
12 13 14 15
)

// ValidatorFunc is a function that is called to validate a given
// type of DHTRecord.
16
type ValidatorFunc func(key.Key, []byte) error
17 18 19 20 21 22 23 24 25 26 27 28

// ErrBadRecord is returned any time a dht record is found to be
// incorrectly formatted or signed.
var ErrBadRecord = errors.New("bad dht record")

// 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")

// Validator is an object that helps ensure routing records are valid.
// It is a collection of validator functions, each of which implements
// its own notion of validity.
29 30 31 32 33 34
type Validator map[string]*ValidChecker

type ValidChecker struct {
	Func ValidatorFunc
	Sign bool
}
35 36 37

// VerifyRecord checks a record and ensures it is still valid.
// It runs needed validators
Jeromy's avatar
Jeromy committed
38
func (v Validator) VerifyRecord(r *pb.Record) error {
39
	// Now, check validity func
rht's avatar
rht committed
40
	parts := path.SplitList(r.GetKey())
41
	if len(parts) < 3 {
42
		log.Infof("Record key does not have validator: %s", key.Key(r.GetKey()))
43 44 45
		return nil
	}

46
	val, ok := v[parts[1]]
47
	if !ok {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
48
		log.Infof("Unrecognized key prefix: %s", parts[1])
49 50 51
		return ErrInvalidRecordType
	}

52
	return val.Func(key.Key(r.GetKey()), r.GetValue())
53 54
}

55
func (v Validator) IsSigned(k key.Key) (bool, error) {
56
	// Now, check validity func
rht's avatar
rht committed
57
	parts := path.SplitList(string(k))
58 59 60 61 62 63 64 65 66 67 68 69
	if len(parts) < 3 {
		log.Infof("Record key does not have validator: %s", k)
		return false, nil
	}

	val, ok := v[parts[1]]
	if !ok {
		log.Infof("Unrecognized key prefix: %s", parts[1])
		return false, ErrInvalidRecordType
	}

	return val.Sign, nil
70 71 72 73 74
}

// ValidatePublicKeyRecord implements ValidatorFunc and
// verifies that the passed in record value is the PublicKey
// that matches the passed in key.
75
func ValidatePublicKeyRecord(k key.Key, val []byte) error {
76 77 78 79 80 81 82 83 84 85 86
	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
}
Jeromy's avatar
Jeromy committed
87

88 89 90 91 92
var PublicKeyValidator = &ValidChecker{
	Func: ValidatePublicKeyRecord,
	Sign: false,
}

Jeromy's avatar
Jeromy committed
93 94 95 96 97 98 99 100 101 102 103
func CheckRecordSig(r *pb.Record, pk ci.PubKey) error {
	blob := RecordBlobForSig(r)
	good, err := pk.Verify(blob, r.Signature)
	if err != nil {
		return nil
	}
	if !good {
		return errors.New("invalid record signature")
	}
	return nil
}