-
Notifications
You must be signed in to change notification settings - Fork 7
/
hidden.go
308 lines (270 loc) · 8.99 KB
/
hidden.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Package hidden is a manager for hidden services.
package hidden
import (
"github.com/indra-labs/indra/pkg/codec/ad/intro"
"github.com/indra-labs/indra/pkg/crypto"
"github.com/indra-labs/indra/pkg/crypto/ciph"
"github.com/indra-labs/indra/pkg/crypto/nonce"
"github.com/indra-labs/indra/pkg/engine/consts"
"github.com/indra-labs/indra/pkg/engine/services"
log2 "github.com/indra-labs/indra/pkg/proc/log"
"github.com/indra-labs/indra/pkg/util/slice"
"github.com/indra-labs/indra/pkg/util/splice"
"sync"
)
var (
log = log2.GetLogger()
fails = log.E.Chk
)
// Hidden is a collection of data related to hidden services.
// Introductions both own and other, hidden services.
type Hidden struct {
// Mutex because this needs to be concurrent safe.
sync.Mutex
// MyIntros are hidden services being hosted in this current engine.
MyIntros
// KnownIntros are hidden services we have heard of and can return to a query if we have them.
//
// todo: this probably belongs with the peerstore...
KnownIntros
// Services are the service specifications for the services we are providing from this engine.
Services
}
// AddHiddenService adds a new hidden service with a given private key, intro and address.
//
// todo: looks like that addr parameter should be in a logging closure derived from the key.
func (hr *Hidden) AddHiddenService(svc *services.Service, key *crypto.Prv,
in *intro.Ad, addr string) {
pk := crypto.DerivePub(key).ToBytes()
hr.Lock()
log.D.F("%s added hidden service with key %s", addr, pk)
hr.Services[pk] = &LocalHiddenService{
Prv: key,
Service: svc,
}
hr.Services[pk].CurrentIntros = append(hr.Services[pk].
CurrentIntros, in)
hr.Unlock()
}
// AddIntro adds an intro for a newly created hidden service.
func (hr *Hidden) AddIntro(pk *crypto.Pub, intro *Introduction) {
hr.Lock()
var ok bool
key := pk.ToBytes()
if _, ok = hr.MyIntros[key]; ok {
log.D.Ln("entry already exists for key %x", key)
} else {
hr.MyIntros[key] = intro
}
hr.Unlock()
}
// AddIntroToHiddenService adds an intro to a given hidden service.
//
// todo: this looks like it isn't used.
func (hr *Hidden) AddIntroToHiddenService(key crypto.PubBytes, in *intro.Ad) {
hr.Lock()
hr.Services[key].CurrentIntros = append(hr.Services[key].CurrentIntros, in)
hr.Unlock()
}
// Delete removes a hidden service identified by its public key bytes.
func (hr *Hidden) Delete(key crypto.PubBytes) (header *Introduction) {
hr.Lock()
var ok bool
if header, ok = hr.MyIntros[key]; ok {
delete(hr.MyIntros, key)
}
hr.Unlock()
return
}
// DeleteIntroByID removes the Intro with matching nonce.ID.
func (hr *Hidden) DeleteIntroByID(id nonce.ID) {
hr.Lock()
out:
for i := range hr.Services {
for j := range hr.Services[i].CurrentIntros {
if hr.Services[i].CurrentIntros[j].ID == id {
tmp := hr.Services[i].CurrentIntros
tmp = append(tmp[:j], tmp[j+1:]...)
hr.Services[i].CurrentIntros = tmp
break out
}
}
}
for i := range hr.KnownIntros {
if hr.KnownIntros[i].ID == id {
delete(hr.KnownIntros, i)
break
}
}
hr.Unlock()
}
// DeleteKnownIntro evicts a known intro.
//
// todo: this really should be integrated with the peerstore.
func (hr *Hidden) DeleteKnownIntro(key crypto.PubBytes) (
header *Introduction) {
hr.Lock()
var ok bool
if _, ok = hr.KnownIntros[key]; ok {
delete(hr.KnownIntros, key)
}
hr.Unlock()
return
}
// RoutingHeaderBytes is a raw bytes form of a 3 layer RoutingHeader.
type RoutingHeaderBytes [consts.RoutingHeaderLen]byte
// Services is a map of local hidden services keyed to their public key.
type Services map[crypto.PubBytes]*LocalHiddenService
// FormatReply constructs a reply message with RoutingHeader, ciphers and nonces,
// and the response payload, encrypting it and writing it into a splice.
func FormatReply(header RoutingHeaderBytes, ciphers crypto.Ciphers,
nonces crypto.Nonces, res slice.Bytes) (rb *splice.Splice) {
rl := consts.RoutingHeaderLen
rb = splice.New(rl + len(res))
copy(rb.GetUntil(rl), header[:rl])
copy(rb.GetFrom(rl), res)
for i := range ciphers {
blk := ciph.BlockFromHash(ciphers[i])
ciph.Encipher(blk, nonces[i], rb.GetFrom(rl))
}
return
}
// GetRoutingHeaderFromCursor extracts what should be a routing header from a
// splice.
func GetRoutingHeaderFromCursor(s *splice.Splice) (r RoutingHeaderBytes) {
rh := s.GetRange(s.GetCursor(), s.Advance(consts.RoutingHeaderLen,
"routing header"))
copy(r[:], rh)
return
}
// FindCloakedHiddenService checks known local hidden service keys to match a
// "To" cloaked public key, for which we should have the private key to form the
// ECDH secret to decrypt the message.
func (hr *Hidden) FindCloakedHiddenService(key crypto.CloakedPubKey) (
pubKey *crypto.PubBytes) {
for i := range hr.MyIntros {
pubKey1 := hr.MyIntros[i].Intro.Key.ToBytes()
if crypto.Match(key, pubKey1) {
return &pubKey1
}
}
for i := range hr.Services {
if crypto.Match(key, i) {
return &i
}
}
for i := range hr.KnownIntros {
if crypto.Match(key, i) {
return &i
}
}
return
}
// FindHiddenService searches for a hidden service from local hidden services.
func (hr *Hidden) FindHiddenService(key crypto.PubBytes) (
hs *LocalHiddenService) {
hr.Lock()
var ok bool
if hs, ok = hr.Services[key]; ok {
}
hr.Unlock()
return
}
// FindIntroduction returns the intro.Ad matching a provided public key bytes.
func (hr *Hidden) FindIntroduction(key crypto.PubBytes) (intro *Introduction) {
hr.Lock()
intro = hr.FindIntroductionUnsafe(key)
hr.Unlock()
return
}
// FindIntroductionUnsafe does the same thing as FindIntroduction without
// locking. Used when the lock is already acquired.
func (hr *Hidden) FindIntroductionUnsafe(
key crypto.PubBytes) (intro *Introduction) {
var ok bool
if intro, ok = hr.MyIntros[key]; ok {
}
return
}
// FindKnownIntro searches non-local intros for a matching public key bytes.
//
// todo: this definitely should be part of peerstore.
func (hr *Hidden) FindKnownIntro(key crypto.PubBytes) (intro *intro.Ad) {
hr.Lock()
intro = hr.FindKnownIntroUnsafe(key)
hr.Unlock()
return
}
// FindKnownIntroUnsafe searches for a KnownIntro without locking.
func (hr *Hidden) FindKnownIntroUnsafe(key crypto.PubBytes) (intro *intro.Ad) {
var ok bool
if intro, ok = hr.KnownIntros[key]; ok {
}
return
}
// Introduction is the combination of an intro.Ad and a ReplyHeader, used to
// forward the Route message from a client and establish the connection between
// client and hidden service.
type Introduction struct {
Intro *intro.Ad
ReplyHeader
}
// KnownIntros is a key/value store of hidden service intros we know of.
//
// todo: This definitely should be peerstore
type KnownIntros map[crypto.PubBytes]*intro.Ad
// LocalHiddenService is a hidden service being served from this node.
type LocalHiddenService struct {
// Prv is the private key for the hidden service.
Prv *crypto.Prv
// CurrentIntros are intro.Ad that are current for this hidden service. Not sure
// yet how many this should be. 6 or more, it really depends, perhaps have it
// scale up if demand exceeds supply to some sort of reasonable ceiling.
CurrentIntros []*intro.Ad
// Service is the definition of the hidden service. There should be a server
// listening on or forwarding from the service port on localhost that provides
// the service.
*services.Service
}
// MyIntros is a key value store of the hidden service introductions we have got
// currently available and sent out to introducing nodes.
type MyIntros map[crypto.PubBytes]*Introduction
// ReplyHeader is the bundle of routing header, payload encryption secrets and
// the nonces to be used, which match also what is inside the RoutingHeaderBytes.
type ReplyHeader struct {
// RoutingHeaderBytes contains the 3 layer RoutingHeader that holds the path
// instructions in three progressively encrypted layers in reverse order so to be
// unwrapped progressively.
RoutingHeaderBytes
// Ciphers is a set of 3 symmetric ciphers that are to be used in their
// given order over the reply message from the service.
crypto.Ciphers
// Nonces are the nonces to use with the cipher when creating the
// encryption for the reply message,
// they are common with the crypts in the header.
crypto.Nonces
}
// NewHiddenRouting instantiates a new Hidden for managing hidden services.
func NewHiddenRouting() *Hidden {
return &Hidden{
MyIntros: make(MyIntros),
KnownIntros: make(KnownIntros),
Services: make(Services),
}
}
// ReadRoutingHeader extracts a RoutingHeaderBytes from a splice at the current
// cursor.
func ReadRoutingHeader(s *splice.Splice, b *RoutingHeaderBytes) *splice.Splice {
*b = GetRoutingHeaderFromCursor(s)
s.Segments = append(s.Segments,
splice.NameOffset{Offset: s.GetCursor(), Name: "routingheader"})
return s
}
// WriteRoutingHeader copies RoutingHeaderBytes into a splice.
func WriteRoutingHeader(s *splice.Splice, b RoutingHeaderBytes) *splice.Splice {
copy(s.GetAll()[s.GetCursor():s.Advance(consts.RoutingHeaderLen,
"routing header")], b[:])
s.Segments = append(s.Segments,
splice.NameOffset{Offset: s.GetCursor(), Name: "routingheader"})
return s
}