This repository has been archived by the owner on Jun 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmixkey.go
176 lines (149 loc) · 3.96 KB
/
mixkey.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
// mixkey.go - Katzenpost server mix key store.
// 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 server
import (
"fmt"
"os"
"path/filepath"
"sync"
"github.com/katzenpost/core/crypto/ecdh"
"github.com/katzenpost/core/epochtime"
"github.com/katzenpost/server/internal/constants"
"github.com/katzenpost/server/internal/glue"
"github.com/katzenpost/server/internal/mixkey"
"gopkg.in/op/go-logging.v1"
)
type mixKeys struct {
sync.Mutex
glue glue.Glue
log *logging.Logger
keys map[uint64]*mixkey.MixKey
}
func (m *mixKeys) init() error {
// Generate/load the initial set of keys.
//
// TODO: In theory this should also try to load the previous epoch's key
// if the current time is in the clock skew grace period. But it may not
// matter much in practice.
epoch, _, _ := epochtime.Now()
if _, err := m.Generate(epoch); err != nil {
return err
}
// Clean up stale mix keys hanging around the data directory.
files, err := filepath.Glob(filepath.Join(m.glue.Config().Server.DataDir, mixkey.KeyGlob))
if err != nil {
m.log.Warningf("Failed to find persisted keys: %v", err)
}
keyFmt := filepath.Join(m.glue.Config().Server.DataDir, mixkey.KeyFmt)
for _, f := range files {
e := uint64(0)
if _, err := fmt.Sscanf(f, keyFmt, &e); err != nil {
m.log.Debugf("Failed to extract epoch from '%v': %v", f, err)
continue
}
if _, ok := m.keys[e]; !ok && e < epoch {
m.log.Debugf("Purging stale key: %v", f)
os.Remove(f)
}
}
return nil
}
func (m *mixKeys) Generate(baseEpoch uint64) (bool, error) {
didGenerate := false
m.Lock()
defer m.Unlock()
for e := baseEpoch; e < baseEpoch+constants.NumMixKeys; e++ {
// Skip keys that we already have.
if _, ok := m.keys[e]; ok {
continue
}
didGenerate = true
k, err := mixkey.New(m.glue.Config().Server.DataDir, e)
if err != nil {
// Clean up whatever keys that may have succeeded.
for ee := baseEpoch; ee < baseEpoch+constants.NumMixKeys; ee++ {
if kk, ok := m.keys[ee]; ok {
kk.Deref()
delete(m.keys, ee)
}
}
return false, err
}
k.SetUnlinkIfExpired(true)
m.keys[e] = k
}
return didGenerate, nil
}
func (m *mixKeys) Prune() bool {
epoch, _, _ := epochtime.Now()
didPrune := false
m.Lock()
defer m.Unlock()
for idx, v := range m.keys {
if idx < epoch-1 {
m.log.Debugf("Purging expired key for epoch: %v", idx)
v.Deref()
delete(m.keys, idx)
didPrune = true
}
}
return didPrune
}
func (m *mixKeys) Get(epoch uint64) (*ecdh.PublicKey, bool) {
m.Lock()
defer m.Unlock()
if k, ok := m.keys[epoch]; ok {
return k.PublicKey(), true
}
return nil, false
}
func (m *mixKeys) Shadow(dst map[uint64]*mixkey.MixKey) {
m.Lock()
defer m.Unlock()
// Purge the keys no longer listed from dst.
for k, v := range dst {
if _, ok := m.keys[k]; !ok {
v.Deref()
delete(dst, k)
}
}
// Add newly listed keys to dst and bump up the refcount.
for k, v := range m.keys {
if _, ok := dst[k]; !ok {
v.Ref()
dst[k] = v
}
}
}
func (m *mixKeys) Halt() {
m.Lock()
defer m.Unlock()
for k, v := range m.keys {
v.Deref()
delete(m.keys, k)
}
}
func newMixKeys(glue glue.Glue) (glue.MixKeys, error) {
m := &mixKeys{
glue: glue,
log: glue.LogBackend().GetLogger("mixkeys"),
keys: make(map[uint64]*mixkey.MixKey),
}
if err := m.init(); err != nil {
return nil, err
}
return m, nil
}