This repository has been archived by the owner on Oct 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
config.go
440 lines (385 loc) · 12.2 KB
/
config.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// config.go - Katzenpost client configuration.
// Copyright (C) 2018 Yawning Angel, David Stainton.
//
// 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 config implements the configuration for the Katzenpost client.
package config
import (
"errors"
"fmt"
"io/ioutil"
"net/mail"
"strings"
"github.com/BurntSushi/toml"
nvClient "github.com/katzenpost/authority/nonvoting/client"
vClient "github.com/katzenpost/authority/voting/client"
vServerConfig "github.com/katzenpost/authority/voting/server/config"
"github.com/katzenpost/client/internal/proxy"
"github.com/katzenpost/core/crypto/eddsa"
"github.com/katzenpost/core/log"
"github.com/katzenpost/core/pki"
registration "github.com/katzenpost/registration_client"
"golang.org/x/net/idna"
"golang.org/x/text/secure/precis"
)
const (
defaultLogLevel = "NOTICE"
defaultPollingInterval = 10
defaultInitialMaxPKIRetrievalDelay = 30
defaultSessionDialTimeout = 30
)
var defaultLogging = Logging{
Disable: false,
File: "",
Level: defaultLogLevel,
}
// Logging is the logging configuration.
type Logging struct {
// Disable disables logging entirely.
Disable bool
// File specifies the log file, if omitted stdout will be used.
File string
// Level specifies the log level.
Level string
}
func (lCfg *Logging) validate() error {
lvl := strings.ToUpper(lCfg.Level)
switch lvl {
case "ERROR", "WARNING", "NOTICE", "INFO", "DEBUG":
case "":
lCfg.Level = defaultLogLevel
default:
return fmt.Errorf("config: Logging: Level '%v' is invalid", lCfg.Level)
}
lCfg.Level = lvl // Force uppercase.
return nil
}
// Debug is the debug configuration.
type Debug struct {
DisableDecoyTraffic bool
// SessionDialTimeout is the number of seconds that a session dial
// is allowed to take until it is canceled.
SessionDialTimeout int
// InitialMaxPKIRetrievalDelay is the initial maximum number of seconds
// we are willing to wait for the retreival of the PKI document.
InitialMaxPKIRetrievalDelay int
// CaseSensitiveUserIdentifiers disables the forced lower casing of
// the Account `User` field.
CaseSensitiveUserIdentifiers bool
// PollingInterval is the interval in seconds that will be used to
// poll the receive queue. By default this is 10 seconds. Reducing
// the value too far WILL result in unnecessary Provider load, and
// increasing the value too far WILL adversely affect large message
// transmit performance.
PollingInterval int
// PreferedTransports is a list of the transports will be used to make
// outgoing network connections, with the most prefered first.
PreferedTransports []pki.Transport
}
func (d *Debug) fixup() {
if d.PollingInterval == 0 {
d.PollingInterval = defaultPollingInterval
}
if d.InitialMaxPKIRetrievalDelay == 0 {
d.InitialMaxPKIRetrievalDelay = defaultInitialMaxPKIRetrievalDelay
}
if d.SessionDialTimeout == 0 {
d.SessionDialTimeout = defaultSessionDialTimeout
}
}
// NonvotingAuthority is a non-voting authority configuration.
type NonvotingAuthority struct {
// Address is the IP address/port combination of the authority.
Address string
// PublicKey is the authority's public key.
PublicKey *eddsa.PublicKey
}
// New constructs a pki.Client with the specified non-voting authority config.
func (nvACfg *NonvotingAuthority) New(l *log.Backend, pCfg *proxy.Config) (pki.Client, error) {
cfg := &nvClient.Config{
LogBackend: l,
Address: nvACfg.Address,
PublicKey: nvACfg.PublicKey,
DialContextFn: pCfg.ToDialContext("nonvoting:" + nvACfg.PublicKey.String()),
}
return nvClient.New(cfg)
}
func (nvACfg *NonvotingAuthority) validate() error {
if nvACfg.PublicKey == nil {
return errors.New("error PublicKey is missing")
}
return nil
}
// VotingAuthority is a voting authority configuration.
type VotingAuthority struct {
Peers []*vServerConfig.AuthorityPeer
}
// New constructs a pki.Client with the specified non-voting authority config.
func (vACfg *VotingAuthority) New(l *log.Backend, pCfg *proxy.Config) (pki.Client, error) {
cfg := &vClient.Config{
LogBackend: l,
Authorities: vACfg.Peers,
DialContextFn: pCfg.ToDialContext("voting"),
}
return vClient.New(cfg)
}
func (vACfg *VotingAuthority) validate() error {
if vACfg.Peers == nil || len(vACfg.Peers) == 0 {
return errors.New("error VotingAuthority failure, must specify at least one peer")
}
for _, peer := range vACfg.Peers {
if peer.IdentityPublicKey == nil || peer.LinkPublicKey == nil || len(peer.Addresses) == 0 {
return errors.New("invalid voting authority peer")
}
}
return nil
}
// NewPKIClient returns a voting or nonvoting implementation of pki.Client or error
func (c *Config) NewPKIClient(l *log.Backend, pCfg *proxy.Config) (pki.Client, error) {
switch {
case c.NonvotingAuthority != nil:
return c.NonvotingAuthority.New(l, pCfg)
case c.VotingAuthority != nil:
return c.VotingAuthority.New(l, pCfg)
}
return nil, errors.New("no Authority found")
}
// Reunion is the Reunion configuration needed by clients
// in order to use the Reunion service
type Reunion struct {
// Enable indicates that the reunion protocol should be used
Enable bool
}
func (r *Reunion) validate() error {
// stub for future options, e.g. alternate clocks, etc
return nil
}
// Panda is the PANDA configuration needed by clients
// in order to use the PANDA service
type Panda struct {
// Receiver is the recipient ID that shall receive the Sphinx packets destined
// for this PANDA service.
Receiver string
// Provider is the Provider on this mix network which is hosting this PANDA service.
Provider string
// BlobSize is the size of the PANDA blobs that clients will use.
BlobSize int
}
func (p *Panda) validate() error {
if p.Receiver == "" {
return errors.New("receiver is missing")
}
if p.Provider == "" {
return errors.New("provider is missing")
}
return nil
}
// Account is a provider account configuration.
type Account struct {
// User is the account user name.
User string
// Provider is the provider identifier used by this account.
Provider string
// ProviderKeyPin is the optional pinned provider signing key.
ProviderKeyPin *eddsa.PublicKey
}
func (accCfg *Account) fixup(cfg *Config) error {
var err error
if !cfg.Debug.CaseSensitiveUserIdentifiers {
accCfg.User, err = precis.UsernameCaseMapped.String(accCfg.User)
} else {
accCfg.User, err = precis.UsernameCasePreserved.String(accCfg.User)
}
if err != nil {
return err
}
accCfg.Provider, err = idna.Lookup.ToASCII(accCfg.Provider)
return err
}
func (accCfg *Account) toEmailAddr() (string, error) {
addr := fmt.Sprintf("%s@%s", accCfg.User, accCfg.Provider)
if _, err := mail.ParseAddress(addr); err != nil {
return "", fmt.Errorf("error User/Provider does not form a valid e-mail address: %v", err)
}
return addr, nil
}
func (accCfg *Account) validate() error {
if accCfg.User == "" {
return errors.New("user is missing")
}
if accCfg.Provider == "" {
return errors.New("provider is missing")
}
return nil
}
// Registration is used for the client's Provider account registration.
type Registration struct {
Address string
Options *registration.Options
}
func (r *Registration) validate() error {
if r.Address == "" {
return errors.New("registration Address cannot be empty")
}
return nil
}
// UpstreamProxy is the outgoing connection proxy configuration.
type UpstreamProxy struct {
// Type is the proxy type (Eg: "none"," socks5").
Type string
// Network is the proxy address' network (`unix`, `tcp`).
Network string
// Address is the proxy's address.
Address string
// User is the optional proxy username.
User string
// Password is the optional proxy password.
Password string
}
func (uCfg *UpstreamProxy) toProxyConfig() (*proxy.Config, error) {
// This is kind of dumb, but this is the cleanest way I can think of
// doing this.
cfg := &proxy.Config{
Type: uCfg.Type,
Network: uCfg.Network,
Address: uCfg.Address,
User: uCfg.User,
Password: uCfg.Password,
}
if err := cfg.FixupAndValidate(); err != nil {
return nil, err
}
return cfg, nil
}
// Config is the top level client configuration.
type Config struct {
Logging *Logging
UpstreamProxy *UpstreamProxy
Debug *Debug
NonvotingAuthority *NonvotingAuthority
VotingAuthority *VotingAuthority
Account *Account
Registration *Registration
Panda *Panda
Reunion *Reunion
upstreamProxy *proxy.Config
}
// UpstreamProxyConfig returns the configured upstream proxy, suitable for
// internal use. Most people should not use this.
func (c *Config) UpstreamProxyConfig() *proxy.Config {
return c.upstreamProxy
}
// FixupAndMinimallyValidate applies defaults to config entries and validates the
// all but the Account and Registration configuration sections.
func (c *Config) FixupAndMinimallyValidate() error {
// Handle missing sections if possible.
if c.Logging == nil {
c.Logging = &defaultLogging
}
if c.Debug == nil {
c.Debug = &Debug{
PollingInterval: defaultPollingInterval,
InitialMaxPKIRetrievalDelay: defaultInitialMaxPKIRetrievalDelay,
}
} else {
c.Debug.fixup()
}
// Validate/fixup the various sections.
if err := c.Logging.validate(); err != nil {
return err
}
if uCfg, err := c.UpstreamProxy.toProxyConfig(); err == nil {
c.upstreamProxy = uCfg
} else {
return err
}
switch {
case c.NonvotingAuthority == nil && c.VotingAuthority != nil:
if err := c.VotingAuthority.validate(); err != nil {
return fmt.Errorf("config: NonvotingAuthority/VotingAuthority is invalid: %s", err)
}
case c.NonvotingAuthority != nil && c.VotingAuthority == nil:
if err := c.NonvotingAuthority.validate(); err != nil {
return fmt.Errorf("config: NonvotingAuthority/VotingAuthority is invalid: %s", err)
}
default:
return fmt.Errorf("config: Authority configuration is invalid")
}
// Panda is optional
if c.Panda != nil {
err := c.Panda.validate()
if err != nil {
return fmt.Errorf("config: Panda config is invalid: %v", err)
}
}
// Reunion is optional
if c.Reunion != nil {
err := c.Reunion.validate()
if err != nil {
return fmt.Errorf("config: Reunion config is invalid: %v", err)
}
}
return nil
}
// FixupAndValidate applies defaults to config entries and validates the
// supplied configuration. Most people should call one of the Load variants
// instead.
func (c *Config) FixupAndValidate() error {
err := c.FixupAndMinimallyValidate()
if err != nil {
return err
}
// Account
if err := c.Account.fixup(c); err != nil {
return fmt.Errorf("config: Account is invalid (User): %v", err)
}
addr, err := c.Account.toEmailAddr()
if err != nil {
return fmt.Errorf("config: Account is invalid (Identifier): %v", err)
}
if err := c.Account.validate(); err != nil {
return fmt.Errorf("config: Account '%v' is invalid: %v", addr, err)
}
// Registration
if c.Registration == nil {
return errors.New("config: error, Registration config section is non-optional")
}
err = c.Registration.validate()
return err
}
// Load parses and validates the provided buffer b as a config file body and
// returns the Config.
func Load(b []byte) (*Config, error) {
cfg := new(Config)
md, err := toml.Decode(string(b), cfg)
if err != nil {
return nil, err
}
if undecoded := md.Undecoded(); len(undecoded) != 0 {
return nil, fmt.Errorf("config: Undecoded keys in config file: %v", undecoded)
}
if err := cfg.FixupAndMinimallyValidate(); err != nil {
return nil, err
}
return cfg, nil
}
// LoadFile loads, parses, and validates the provided file and returns the
// Config.
func LoadFile(f string) (*Config, error) {
b, err := ioutil.ReadFile(f)
if err != nil {
return nil, err
}
return Load(b)
}