aboutsummaryrefslogtreecommitdiff
path: root/src/gnunet/service/dht/blocks/gns.go
blob: 208567752fa4ac5d7761164a85a1833890b194a2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// This file is part of gnunet-go, a GNUnet-implementation in Golang.
// Copyright (C) 2019-2022 Bernd Fix  >Y<
//
// gnunet-go is free software: you can redistribute it and/or modify it
// under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// gnunet-go is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: AGPL3.0-or-later

package blocks

import (
	"errors"
	"fmt"
	"gnunet/crypto"
	"gnunet/util"

	"github.com/bfix/gospel/data"
)

// Error messages
var (
	ErrBlockNotDecrypted = fmt.Errorf("GNS block not decrypted")
)

//----------------------------------------------------------------------
// Query key for GNS lookups
//----------------------------------------------------------------------

// GNSQuery specifies the context for a basic GNS name lookup of an (atomic)
// label in a given zone identified by its public key.
type GNSQuery struct {
	GenericQuery
	Zone    *crypto.ZoneKey // Public zone key
	Label   string          // Atomic label
	derived *crypto.ZoneKey // Derived zone key from (pkey,label)
}

// Verify the integrity of the block data from a signature.
func (q *GNSQuery) Verify(b Block) (err error) {
	switch blk := b.(type) {
	case *GNSBlock:
		// Integrity check performed
		blk.checked = true

		// verify derived key
		dkey := blk.DerivedKeySig.ZoneKey
		dkey2, _ := q.Zone.Derive(q.Label, "gns")
		if !dkey.Equal(dkey2) {
			return fmt.Errorf("invalid signature key for GNS Block")
		}
		// verify signature
		var buf []byte
		if buf, err = data.Marshal(blk.Body); err != nil {
			return
		}
		blk.verified, err = blk.DerivedKeySig.Verify(buf)

	default:
		err = errors.New("can't verify block type")
	}
	return
}

// Decrypt block data with a key derived from zone key and label.
func (q *GNSQuery) Decrypt(b Block) (err error) {
	switch blk := b.(type) {
	case *GNSBlock:
		// decrypt GNS payload
		blk.data, err = q.Zone.Decrypt(blk.Body.Data, q.Label, blk.Body.Expire)
		blk.decrypted = true
		return

	default:
		err = errors.New("can't decrypt block type")
	}
	return
}

// NewGNSQuery assembles a new Query object for the given zone and label.
func NewGNSQuery(zkey *crypto.ZoneKey, label string) *GNSQuery {
	// derive a public key from (pkey,label) and set the repository
	// key as the SHA512 hash of the binary key representation.
	// (key blinding)
	pd, _ := zkey.Derive(label, "gns")
	gq := crypto.Hash(pd.Bytes()).Bits
	return &GNSQuery{
		GenericQuery: *NewGenericQuery(gq),
		Zone:         zkey,
		Label:        label,
		derived:      pd,
	}
}

//----------------------------------------------------------------------
// GNS blocks
//----------------------------------------------------------------------

// SignedGNSBlockData represents the signed content of a GNS block
type SignedGNSBlockData struct {
	Purpose *crypto.SignaturePurpose ``         // Size and purpose of signature (8 bytes)
	Expire  util.AbsoluteTime        ``         // Expiration time of the block.
	Data    []byte                   `size:"*"` // Block data content
}

// GNSBlock is the result of GNS lookups for a given label in a zone.
// An encrypted and signed container for GNS resource records that represents
// the "atomic" data structure associated with a GNS label in a given zone.
type GNSBlock struct {
	GenericBlock

	// persistent
	DerivedKeySig *crypto.ZoneSignature // Derived key used for signing
	Body          *SignedGNSBlockData

	// transient data (not serialized)
	checked   bool   // block integrity checked
	verified  bool   // block signature verified (internal)
	decrypted bool   // block decrypted (internal)
	data      []byte // decrypted data
}

// Data block interface implementation
func (b *GNSBlock) Data() []byte {
	buf, _ := data.Marshal(b)
	return buf
}

// String returns the human-readable representation of a GNSBlock
func (b *GNSBlock) String() string {
	return fmt.Sprintf("GNSBlock{Verified=%v,Decrypted=%v,data=[%d]}",
		b.verified, b.decrypted, len(b.Body.Data))
}

// NewBlock instantiates an empty GNS block
func NewBlock() *GNSBlock {
	return &GNSBlock{
		DerivedKeySig: nil,
		Body: &SignedGNSBlockData{
			Purpose: new(crypto.SignaturePurpose),
			Expire:  *new(util.AbsoluteTime),
			Data:    nil,
		},
		checked:   false,
		verified:  false,
		decrypted: false,
		data:      nil,
	}
}

// Verify the integrity of the block data from a signature.
// Only the cryptographic signature is verified; the formal correctness of
// the association between the block and a GNS label in a GNS zone can't
// be verified. This is only possible in Query.Verify().
func (b *GNSBlock) Verify() (err error) {
	// verify signature
	var buf []byte
	if buf, err = data.Marshal(b.Body); err != nil {
		return
	}
	_, err = b.DerivedKeySig.Verify(buf)
	return
}