forked from keys-pub/keys-ext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
266 lines (241 loc) · 5.97 KB
/
auth.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
package vault
import (
"github.com/burlingtonbertie99/mykeys"
"github.com/burlingtonbertie99/mykeys/dstore"
"github.com/pkg/errors"
)
// ErrInvalidAuth if auth is invalid.
var ErrInvalidAuth = errors.New("invalid auth")
// ErrLocked if you try to access vault while it is locked.
var ErrLocked = errors.New("vault is locked")
// ErrAlreadySetup if already setup.
var ErrAlreadySetup = errors.New("vault is already setup")
// Salt is default salt value, generated on first access.
// This salt value is not encrypted.
// Doesn't require Unlock().
func (v *Vault) Salt() ([]byte, error) {
path := dstore.Path("config", "salt")
salt, err := v.store.Get(path)
if err != nil {
return nil, err
}
if salt == nil {
salt = keys.Rand32()[:]
if err := v.set(path, salt, true); err != nil {
return nil, err
}
}
return salt, nil
}
// AuthType describes an auth method.
type AuthType string
const (
// UnknownAuth ...
UnknownAuth AuthType = ""
// PaperKeyAuth ...
PaperKeyAuth AuthType = "paper-key"
// PasswordAuth ...
PasswordAuth AuthType = "password"
// FIDO2HMACSecretAuth ...
FIDO2HMACSecretAuth AuthType = "fido2-hmac-secret" // #nosec
)
// Status for vault.
type Status string
const (
// Unknown status.
Unknown Status = ""
// SetupNeeded if setup needed.
SetupNeeded Status = "setup-needed"
// Unlocked if unlocked.
Unlocked Status = "unlocked"
// Locked if locked.
Locked Status = "locked"
)
// Status returns vault status.
// If there are no auths or provisions, returns vault.Setup.
// Doesn't require Unlock().
// TODO: We may want to re-think hardware provisioning requiring seperate Unlock step on setup.
func (v *Vault) Status() (Status, error) {
if v.mk != nil {
return Unlocked, nil
}
authed, err := v.hasAuth()
if err != nil {
return Unknown, err
}
if !authed {
return SetupNeeded, nil
}
return Locked, nil
}
// Setup auth, if no auth exists.
// Returns ErrAlreadySetup if already setup.
// Doesn't require Unlock().
func (v *Vault) Setup(key *[32]byte, provision *Provision) error {
status, err := v.Status()
if err != nil {
return err
}
if status != SetupNeeded {
return ErrAlreadySetup
}
if provision == nil {
return errors.Errorf("no provision")
}
if _, err := v.authSetup(provision.ID, key); err != nil {
return err
}
if err := v.provisionSave(provision); err != nil {
return err
}
logger.Infof("Vault auth setup with %s", provision.ID)
return nil
}
// Unlock with auth.
// Returns provision used to unlock.
func (v *Vault) Unlock(key *[32]byte) (*Provision, error) {
// TODO: This can be called while already unlocked, which should be ok,
// but maybe be more explicit about it?
logger.Infof("Unlocking...")
id, mk, err := v.authUnlock(key)
if err != nil {
return nil, err
}
if mk == nil {
logger.Infof("Unlock failed")
return nil, ErrInvalidAuth
}
logger.Infof("Unlocked with %s", id)
if err := v.setMasterKey(mk); err != nil {
return nil, err
}
provision, err := v.provision(id)
if err != nil {
return nil, err
}
if provision == nil {
provision = &Provision{ID: id}
}
return provision, nil
}
// Lock the vault.
func (v *Vault) Lock() {
v.mk = nil
v.remote = nil
}
// authSetup creates master key and encrypts it with the auth key.
func (v *Vault) authSetup(id string, key *[32]byte) (*[32]byte, error) {
if id == "" {
return nil, errors.Errorf("no auth id")
}
// MK is the master key, setup creates it.
mk := keys.Rand32()
if err := v.authCreate(id, key, mk); err != nil {
return nil, err
}
return mk, nil
}
// authCreate encrypts master key with auth key.
func (v *Vault) authCreate(id string, key *[32]byte, mk *[32]byte) error {
if mk == nil {
return ErrLocked
}
if id == "" {
return errors.Errorf("no auth id")
}
item := NewItem(id, mk[:], "", v.Now())
b, err := encryptItem(item, key)
if err != nil {
return err
}
if err := v.set(dstore.Path("auth", id), b, true); err != nil {
return err
}
return nil
}
// authDelete removes an auth key.
func (v *Vault) authDelete(id string) (bool, error) {
if id == "" {
return false, errors.Errorf("no auth id")
}
path := dstore.Path("auth", id)
b, err := v.store.Get(path)
if err != nil {
return false, err
}
if b == nil {
return false, nil
}
ok, err := v.store.Delete(path)
if err != nil {
return false, err
}
if !ok {
return false, nil
}
if err := v.addToPush(dstore.Path("auth", id), nil); err != nil {
return true, err
}
return true, nil
}
func (v *Vault) hasAuth() (bool, error) {
path := dstore.Path("auth")
entries, err := v.store.List(&ListOptions{Prefix: path, NoData: true, Limit: 1})
if err != nil {
return false, err
}
return len(entries) > 0, nil
}
// authUnlock returns (id, master key) or ("", nil) if a matching auth
// is not found.
// Auth is found by trying to decrypt auth until successful.
func (v *Vault) authUnlock(key *[32]byte) (string, *[32]byte, error) {
path := dstore.Path("auth")
entries, err := v.store.List(&ListOptions{Prefix: path})
if err != nil {
return "", nil, err
}
for _, entry := range entries {
logger.Debugf("Trying %s", entry.Path)
item, err := decryptItem(entry.Data, key, "")
if err != nil {
continue
}
if item == nil {
continue
}
if len(item.Data) != 32 {
continue
}
// Convert old ids for compatibility.
id := convertID(item.ID)
return id, keys.Bytes32(item.Data), nil
}
return "", nil, nil
}
// UnlockWithPassword unlocks with a password.
// If setup is true, we are setting up the auth for the first time.
// This is a convenience method, calling Setup or Unlock with KeyForPassword using the Salt.
func (v *Vault) UnlockWithPassword(password string, setup bool) error {
if password == "" {
return errors.Errorf("empty password")
}
salt, err := v.Salt()
if err != nil {
return err
}
key, err := keys.KeyForPassword(password, salt)
if err != nil {
return err
}
if setup {
provision := NewProvision(PasswordAuth)
if err := v.Setup(key, provision); err != nil {
return err
}
}
if _, err := v.Unlock(key); err != nil {
return err
}
return nil
}