-
-
Notifications
You must be signed in to change notification settings - Fork 274
/
keys.go
179 lines (160 loc) Β· 4.45 KB
/
keys.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
package cabin
import (
"encoding/base64"
"errors"
"fmt"
"time"
"github.com/safing/jess"
"github.com/safing/jess/tools"
"github.com/safing/portbase/log"
"github.com/safing/portbase/rng"
"github.com/safing/portmaster/spn/hub"
)
type providedExchKeyScheme struct {
id string
securityLevel int //nolint:structcheck // TODO
tool *tools.Tool
}
var (
// validFor defines how long keys are valid for use by clients.
validFor = 48 * time.Hour // 2 days
// renewBeforeExpiry defines the duration how long before expiry keys should be renewed.
renewBeforeExpiry = 24 * time.Hour // 1 day
// burnAfter defines how long after expiry keys are burnt/deleted.
burnAfter = 12 * time.Hour // 1/2 day
// reuseAfter defines how long IDs should be blocked after expiry (and not be reused for new keys).
reuseAfter = 2 * 7 * 24 * time.Hour // 2 weeks
// provideExchKeySchemes defines the jess tools for creating exchange keys.
provideExchKeySchemes = []*providedExchKeyScheme{
{
id: "ECDH-X25519",
securityLevel: 128, // informative only, security level of ECDH-X25519 is fixed
},
// TODO: test with rsa keys
}
)
func initProvidedExchKeySchemes() error {
for _, eks := range provideExchKeySchemes {
tool, err := tools.Get(eks.id)
if err != nil {
return err
}
eks.tool = tool
}
return nil
}
// MaintainExchKeys maintains the exchange keys, creating new ones and
// deprecating and deleting old ones.
func (id *Identity) MaintainExchKeys(newStatus *hub.Status, now time.Time) (changed bool, err error) {
// create Keys map
if id.ExchKeys == nil {
id.ExchKeys = make(map[string]*ExchKey)
}
// lifecycle management
for keyID, exchKey := range id.ExchKeys {
if exchKey.key != nil && now.After(exchKey.Expires.Add(burnAfter)) {
// delete key
err := exchKey.tool.StaticLogic.BurnKey(exchKey.key)
if err != nil {
log.Warningf(
"spn/cabin: failed to burn key %s (%s) of %s: %s",
keyID,
exchKey.tool.Info.Name,
id.Hub.ID,
err,
)
}
// remove reference
exchKey.key = nil
}
if now.After(exchKey.Expires.Add(reuseAfter)) {
// remove key
delete(id.ExchKeys, keyID)
}
}
// find or create current keys
for _, eks := range provideExchKeySchemes {
found := false
for _, exchKey := range id.ExchKeys {
if exchKey.key != nil &&
exchKey.key.Scheme == eks.id &&
now.Before(exchKey.Expires.Add(-renewBeforeExpiry)) {
found = true
break
}
}
if !found {
err := id.createExchKey(eks, now)
if err != nil {
return false, fmt.Errorf("failed to create %s exchange key: %w", eks.tool.Info.Name, err)
}
changed = true
}
}
// export most recent keys to HubStatus
if changed || len(newStatus.Keys) == 0 {
// reset
newStatus.Keys = make(map[string]*hub.Key)
// find longest valid key for every provided scheme
for _, eks := range provideExchKeySchemes {
// find key of scheme that is valid the longest
longestValid := &ExchKey{
Expires: now,
}
for _, exchKey := range id.ExchKeys {
if exchKey.key != nil &&
exchKey.key.Scheme == eks.id &&
exchKey.Expires.After(longestValid.Expires) {
longestValid = exchKey
}
}
// check result
if longestValid.key == nil {
log.Warningf("spn/cabin: could not find export candidate for exchange key scheme %s", eks.id)
continue
}
// export
hubKey, err := longestValid.toHubKey()
if err != nil {
return false, fmt.Errorf("failed to export %s exchange key: %w", longestValid.tool.Info.Name, err)
}
// add
newStatus.Keys[longestValid.key.ID] = hubKey
}
}
return changed, nil
}
func (id *Identity) createExchKey(eks *providedExchKeyScheme, now time.Time) error {
// get ID
var keyID string
for i := 0; i < 1000000; i++ { // not forever
// generate new ID
b, err := rng.Bytes(3)
if err != nil {
return fmt.Errorf("failed to get random data for key ID: %w", err)
}
keyID = base64.RawURLEncoding.EncodeToString(b)
_, exists := id.ExchKeys[keyID]
if !exists {
break
}
}
if keyID == "" {
return errors.New("unable to find available exchange key ID")
}
// generate key
signet := jess.NewSignetBase(eks.tool)
signet.ID = keyID
// TODO: use security level for key generation
if err := signet.GenerateKey(); err != nil {
return fmt.Errorf("failed to get new exchange key: %w", err)
}
// add to key map
id.ExchKeys[keyID] = &ExchKey{
Created: now,
Expires: now.Add(validFor),
key: signet,
tool: eks.tool,
}
return nil
}