-
Notifications
You must be signed in to change notification settings - Fork 15
/
pkicache.go
236 lines (213 loc) · 6.76 KB
/
pkicache.go
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// pkicache.go - Katzenpost server PKI document cache.
// Copyright (C) 2017 Yawning Angel.
//
// This program 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.
//
// This program 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/>.
// Package pkicache provides a rudimentary cached representation of a PKI
// Document suitable for server use.
package pkicache
import (
"fmt"
"github.com/katzenpost/hpqc/hash"
"github.com/katzenpost/hpqc/sign"
"github.com/katzenpost/katzenpost/core/pki"
"github.com/katzenpost/katzenpost/core/sphinx/constants"
)
// Entry is a cached PKI Document.
type Entry struct {
doc *pki.Document
self *pki.MixDescriptor
incoming map[[constants.NodeIDLength]byte]*pki.MixDescriptor
outgoing map[[constants.NodeIDLength]byte]*pki.MixDescriptor
all map[[constants.NodeIDLength]byte]*pki.MixDescriptor
}
// Epoch returns the epoch that the cached PKI document is valid for.
func (e *Entry) Epoch() uint64 {
return e.doc.Epoch
}
// MixMaxDelay returns the MixMaxDelay for the cached PKI document.
func (e *Entry) MuMaxDelay() uint64 {
return e.doc.MuMaxDelay
}
// SendRatePerMinute returns the SendRatePerMinute for the cached PKI document.
func (e *Entry) SendRatePerMinute() uint64 {
return e.doc.SendRatePerMinute
}
// Self returns the descriptor for the current node.
func (e *Entry) Self() *pki.MixDescriptor {
return e.self
}
// Document returns the PKI document backing the Entry.
func (e *Entry) Document() *pki.Document {
return e.doc
}
// GetIncomingByID returns the MixDescriptor for a incoming connection source
// queried by node ID, or nil iff the node ID is not a valid source.
func (e *Entry) GetIncomingByID(id *[constants.NodeIDLength]byte) *pki.MixDescriptor {
desc, ok := e.incoming[*id]
if !ok {
return nil
}
return desc
}
// GetOutgoingByID returns the MixDescriptor for an outgoing connection
// destination queried by node ID, or nil iff the node ID is not a valid
// destination.
func (e *Entry) GetOutgoingByID(id *[constants.NodeIDLength]byte) *pki.MixDescriptor {
desc, ok := e.outgoing[*id]
if !ok {
return nil
}
return desc
}
// GetByID returns the MixDescriptor by node ID, or nil iff the node ID is not
// listed in the document.
func (e *Entry) GetByID(id *[constants.NodeIDLength]byte) *pki.MixDescriptor {
desc, ok := e.all[*id]
if !ok {
return nil
}
return desc
}
// Outgoing returns a slice of all MixDescriptors that describe valid outgoing
// connection destinations.
func (e *Entry) Outgoing() []*pki.MixDescriptor {
l := make([]*pki.MixDescriptor, 0, len(e.outgoing))
for _, v := range e.outgoing {
l = append(l, v)
}
return l
}
func (e *Entry) isOurLayerSane(isGatewayNode, isServiceNode bool) bool {
if isGatewayNode && isServiceNode {
return false
}
if isGatewayNode && !e.self.IsGatewayNode {
return false
}
if isServiceNode && !e.self.IsServiceNode {
return false
}
if !isGatewayNode {
idHash := hash.Sum256(e.self.IdentityKey)
layer, err := e.doc.GetMixLayer(&idHash)
if err != nil || layer == pki.LayerGateway {
return false
}
if !isServiceNode && int(layer) >= len(e.doc.Topology) {
return false
}
}
if !isServiceNode {
idHash := hash.Sum256(e.self.IdentityKey)
layer, err := e.doc.GetMixLayer(&idHash)
if err != nil || layer == pki.LayerService {
return false
}
if !isGatewayNode && int(layer) >= len(e.doc.Topology) {
return false
}
}
return true
}
func (e *Entry) incomingLayer() []uint8 {
idHash := hash.Sum256(e.self.IdentityKey)
layer, err := e.doc.GetMixLayer(&idHash)
if err != nil {
panic(err)
}
switch layer {
case pki.LayerGateway:
fallthrough
case pki.LayerService:
return []uint8{uint8(len(e.doc.Topology)) - 1}
case 0:
return []uint8{pki.LayerGateway, pki.LayerService}
}
return []uint8{layer - 1}
}
func (e *Entry) outgoingLayer() []uint8 {
idHash := hash.Sum256(e.self.IdentityKey)
layer, err := e.doc.GetMixLayer(&idHash)
if err != nil {
panic(err)
}
switch int(layer) {
case len(e.doc.Topology) - 1:
return []uint8{pki.LayerService, pki.LayerGateway}
case pki.LayerService:
return []uint8{0}
case pki.LayerGateway:
return []uint8{0}
}
return []uint8{layer + 1}
}
// Build the maps of peers that will connect to us, and that we will
// connect to.
func (e *Entry) appendMap(layers []uint8, m map[[constants.NodeIDLength]byte]*pki.MixDescriptor) {
for i := 0; i < len(layers); i++ {
var nodes []*pki.MixDescriptor
switch layers[i] {
case pki.LayerGateway:
nodes = e.doc.GatewayNodes
case pki.LayerService:
nodes = e.doc.ServiceNodes
default:
nodes = e.doc.Topology[layers[i]]
}
for _, v := range nodes {
// The concrete PKI implementation is responsible for ensuring
// that documents only contain one descriptor per identity key.
nodeID := hash.Sum256(v.IdentityKey)
m[nodeID] = v
}
}
}
// New constructs a new Entry from a given document.
func New(d *pki.Document, identityKey sign.PublicKey, isGateway, isServiceNode bool) (*Entry, error) {
e := new(Entry)
e.doc = d
e.incoming = make(map[[constants.NodeIDLength]byte]*pki.MixDescriptor)
e.outgoing = make(map[[constants.NodeIDLength]byte]*pki.MixDescriptor)
e.all = make(map[[constants.NodeIDLength]byte]*pki.MixDescriptor)
// Find our descriptor.
var err error
idKeyHash := hash.Sum256From(identityKey)
e.self, err = d.GetNodeByKeyHash(&idKeyHash)
if err != nil {
return nil, err
}
// Ensure that the self descriptor has a sensible layer.
if !e.isOurLayerSane(isGateway, isServiceNode) {
layer, err := e.doc.GetMixLayer(&idKeyHash)
if err != nil {
return nil, err
}
return nil, fmt.Errorf("pkicache: self layer is invalid: %d", layer)
}
// Note that there are two edge cases where we append two layers to the given
// map via our `appendMap` method, the first layer of mix nodes has two input layers:
// gateway nodes and service nodes. Likewise the last layer of mix nodes has two
// output layers: gateway nodes and service nodes.
incomingLayers := e.incomingLayer()
e.appendMap(incomingLayers, e.incoming)
outgoingLayers := e.outgoingLayer()
e.appendMap(outgoingLayers, e.outgoing)
// Build the list of all nodes.
for i := 0; i < len(e.doc.Topology); i++ {
e.appendMap([]uint8{uint8(i)}, e.all)
}
e.appendMap([]uint8{pki.LayerGateway}, e.all)
e.appendMap([]uint8{pki.LayerService}, e.all)
return e, nil
}