/
concrete.go
456 lines (424 loc) · 12.1 KB
/
concrete.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
// +build linux
package ss
import (
"crypto/aes"
"crypto/rand"
"crypto/sha256"
"fmt"
"io"
"time"
"golang.org/x/crypto/hkdf"
dbus "github.com/guelfey/go.dbus"
"github.com/monnand/dhkx"
)
type Prompt struct{ *dbus.Object }
// This runs the prompt.
//
// The prompt will timeout after 1 minute
func (p Prompt) Prompt(window_id string) (dbus.Variant, error) {
// spec: Prompt(IN String window-id);
empty := dbus.Variant{}
conn, err := dbus.SessionBus()
if err != nil {
return empty, err
}
cmp := make(chan *dbus.Signal, 5)
conn.Signal(cmp)
call := p.Call(_PromptPrompt, 0, window_id)
if call.Err != nil {
return empty, call.Err
}
for {
select {
case sig := <-cmp:
if sig.Name == _PromptCompleted {
if sig.Body[0].(bool) {
return empty, PromptDismissed
}
return sig.Body[1].(dbus.Variant), nil
}
case <-time.After(time.Duration(time.Minute)):
err := p.Dismiss()
if err != nil {
panic(err)
}
return empty, Timeout
}
}
}
// Make a prompt go away.
func (p Prompt) Dismiss() error {
// spec: Dismiss(void);
return p.Call(_PromptDismiss, 0).Err
}
type Item struct{ *dbus.Object }
// Use the passed Session to set the Secret in this Item
func (i Item) SetSecret(s Secret) error {
// spec: SetSecret(IN Secret secret);
return simpleCall(i.Path(), _ItemSetSecret, s)
}
// Use the passed Session to retrieve the Secret in this Item
func (i Item) GetSecret(s Session) (Secret, error) {
// spec: GetSecret(IN ObjectPath session, OUT Secret secret);
var ret Secret
call := i.Call(_ItemGetSecret, 0, s.Path())
if call.Err != nil {
return ret, call.Err
}
call.Store(&ret)
return ret, nil
}
// Any prompt should be handled transparently.
func (i Item) Delete() error {
// spec: Delete (OUT ObjectPath Prompt);
return simpleCall(i.Path(), _ItemDelete)
}
func (i Item) Locked() bool {
v, err := i.GetProperty(_ItemLocked)
if err != nil {
panic(err)
}
return v.Value().(bool)
}
func (i Item) Created() time.Time {
v, err := i.GetProperty(_ItemCreated)
if err != nil {
panic(err)
}
return time.Unix(int64(v.Value().(uint64)), 0)
}
func (i Item) Modified() time.Time {
v, err := i.GetProperty(_ItemModified)
if err != nil {
panic(err)
}
return time.Unix(int64(v.Value().(uint64)), 0)
}
func (i Item) GetAttributes() map[string]string {
v, err := i.GetProperty(_ItemAttributes)
if err != nil {
panic(err)
}
return v.Value().(map[string]string)
}
func (i Item) SetAttributes(attr map[string]string) error {
return i.Call(setProp, 0, _Item, "Attributes", attr).Err
}
func (i Item) GetLabel() string {
v, err := i.GetProperty(_ItemLabel)
if err != nil {
panic(err)
}
return v.Value().(string)
}
func (i Item) SetLabel(l string) error {
return i.Call(setProp, 0, _Item, "Label", l).Err
}
type Service struct{ *dbus.Object }
// First argument is the algorithm used. "plain" (AlgoPlain) and
// "dh-ietf1024-sha256-aes128-cbc-pkcs7" (AlgoDH) are supported.
//
// The dbus api has the caller supply their DH public key and returns
// the other side's public key, but this implementation generates a
// new keypair, does the exchange, derives the encryption key, and then
// stores it in the returned Session.
func (s Service) OpenSession(algo string, args ...interface{}) (Session, error) {
// spec: OpenSession(IN String algorithm, IN Variant input, OUT Variant output, OUT ObjectPath result);
var ret Session
conn, err := dbus.SessionBus()
if err != nil {
return ret, err
}
switch algo {
case AlgoPlain:
var discard dbus.Variant
var sessionPath dbus.ObjectPath
err = s.Call(_ServiceOpenSession, 0, algo, dbus.MakeVariant("")).Store(&discard, &sessionPath)
if err != nil {
return ret, err
}
ret = Session{conn.Object(ServiceName, sessionPath), algo, nil}
case AlgoDH:
// see http://standards.freedesktop.org/secret-service/ch07s03.html
var sessionPath dbus.ObjectPath
var srvReply dbus.Variant
var srvPub []byte
symKey := make([]byte, aes.BlockSize)
grp, err := dhkx.GetGroup(2)
if err != nil {
return ret, err
}
privKey, err := grp.GeneratePrivateKey(rand.Reader)
if err != nil {
return ret, err
}
err = s.Call(_ServiceOpenSession, 0, algo, dbus.MakeVariant(privKey.Bytes())).Store(&srvReply, &sessionPath)
if err != nil {
return ret, err
}
srvPub = srvReply.Value().([]byte)
sharedKey, err := grp.ComputeKey(dhkx.NewPublicKey(srvPub), privKey)
if err != nil {
return ret, err
}
_, err = io.ReadFull(hkdf.New(sha256.New, sharedKey.Bytes(), nil, nil), symKey)
ret = Session{conn.Object(ServiceName, sessionPath), algo, symKey}
default:
err = InvalidAlgorithm
}
return ret, err
}
// The first argument is the Label for the collection, and the second is an (optional) alias.
func (s Service) CreateCollection(label, alias string) (Collection, error) {
// spec: CreateCollection(IN Dict<String,Variant> properties, IN String alias, OUT ObjectPath collection, OUT ObjectPath prompt);
var collectionPath, promptPath dbus.ObjectPath
conn, err := dbus.SessionBus()
if err != nil {
return Collection{}, err
}
properties := map[string]dbus.Variant{
_CollectionLabel: dbus.MakeVariant(label),
}
call := s.Call(_ServiceCreateCollection, 0, properties, alias)
if call.Err != nil {
return Collection{}, call.Err
}
err = call.Store(&collectionPath, &promptPath)
if err != nil {
return Collection{}, err
}
if dbus.ObjectPath("/") != collectionPath {
return Collection{conn.Object(ServiceName, collectionPath)}, nil
}
v, err := checkPrompt(promptPath)
if err != nil {
return Collection{}, err
}
return Collection{conn.Object(ServiceName, dbus.ObjectPath(v.Value().(string)))},
fmt.Errorf("unable to create collection")
}
func (s Service) SearchItems(attrs map[string]string) ([]Item, []Item, error) {
// spec: SearchItems(IN Dict<String,String> attributes, OUT Array<ObjectPath> unlocked, OUT Array<ObjectPath> locked);
var unlocked, locked []dbus.ObjectPath
conn, err := dbus.SessionBus()
if err != nil {
return []Item{}, []Item{}, err
}
call := s.Call(_ServiceSearchItems, 0, attrs)
err = call.Store(&unlocked, &locked)
if err != nil {
return []Item{}, []Item{}, err
}
retUnlocked := make([]Item, len(unlocked))
retLocked := make([]Item, len(locked))
for i, v := range unlocked {
retUnlocked[i] = Item{conn.Object(ServiceName, v)}
}
for i, v := range locked {
retLocked[i] = Item{conn.Object(ServiceName, v)}
}
return retUnlocked, retLocked, nil
}
// UNIMPLEMENTED
func (s Service) Unlock(o []dbus.ObjectPath) ([]dbus.ObjectPath, error) {
// spec: Unlock(IN Array<ObjectPath> objects, OUT Array<ObjectPath> unlocked, OUT ObjectPath prompt);
var ret []dbus.ObjectPath
var prompt dbus.ObjectPath
call := s.Call(_ServiceUnlock, 0, o)
if call.Err != nil {
return []dbus.ObjectPath{}, call.Err
}
err := call.Store(&ret, &prompt)
if err != nil {
return []dbus.ObjectPath{}, call.Err
}
v, err := checkPrompt(prompt)
if err != nil {
return []dbus.ObjectPath{}, call.Err
}
for _, o := range v.Value().([]dbus.ObjectPath) {
ret = append(ret, o)
}
return ret, nil
}
// UNIMPLEMENTED
func (s Service) Lock(o []Object) ([]Object, error) {
// spec: Lock(IN Array<ObjectPath> objects, OUT Array<ObjectPath> locked, OUT ObjectPath Prompt);
return nil, nil
}
// The specified action is to return map[ObjectPath]Secret, but map[Label]Secret is much more useful.
func (s Service) GetSecrets(items []Item, ses Session) (map[dbus.ObjectPath]Secret, error) {
// spec: GetSecrets(IN Array<ObjectPath> items, IN ObjectPath session, OUT Dict<ObjectPath,Secret> secrets);
arg := make([]dbus.ObjectPath, len(items))
for i, o := range items {
arg[i] = o.Path()
}
call := s.Call(_ServiceGetSecrets, 0, arg, ses.Path())
if call.Err != nil {
return map[dbus.ObjectPath]Secret{}, call.Err
}
ret := make(map[dbus.ObjectPath]Secret)
err := call.Store(&ret)
return ret, err
}
func (s Service) ReadAlias(a string) (Collection, error) {
// spec: ReadAlias(IN String name, OUT ObjectPath collection);
var path dbus.ObjectPath
conn, err := dbus.SessionBus()
if err != nil {
return Collection{}, err
}
call := s.Call(_ServiceReadAlias, 0, a)
if call.Err != nil {
return Collection{}, call.Err
}
err = call.Store(&path)
if err != nil {
return Collection{}, err
}
return Collection{conn.Object(ServiceName, path)}, nil
}
func (s Service) SetAlias(a string, c Collection) error {
// spec: SetAlias(IN String name, IN ObjectPath collection);
return simpleCall(s.Path(), _ServiceSetAlias, a, c.Path())
}
// List Colletions
func (s Service) Collections() []Collection {
conn, err := dbus.SessionBus()
if err != nil {
panic(err)
}
v, err := s.GetProperty(_ServiceCollections)
if err != nil {
panic(err)
}
paths := v.Value().([]dbus.ObjectPath)
out := make([]Collection, len(paths))
for i, path := range paths {
out[i] = Collection{conn.Object(ServiceName, path)}
}
return out
}
// type Collection implements the org.freedesktop.SecretService.Collection
// interface, using function calls for property accessors/setters
type Collection struct{ *dbus.Object }
func (c Collection) Delete() error {
// spec: Delete(OUT ObjectPath prompt);
return simpleCall(c.Path(), _CollectionDelete)
}
func (c Collection) SearchItems(attr map[string]string) ([]Item, error) {
// spec: SearchItems(IN Dict<String,String> attributes, OUT Array<ObjectPath> results);
i := []Item{}
conn, err := dbus.SessionBus()
if err != nil {
return i, err
}
call := c.Call(_CollectionSearchItems, 0, attr)
if call.Err != nil {
return i, call.Err
}
var value []dbus.ObjectPath
call.Store(&value)
for _, objPath := range value {
i = append(i, Item{conn.Object(ServiceName, objPath)})
}
return i, nil
}
func (c Collection) CreateItem(label string, attr map[string]string, s Secret, replace bool) (Item, error) {
// spec: CreateItem(IN Dict<String,Variant> properties, IN Secret secret, IN Boolean replace, OUT ObjectPath item, OUT ObjectPath prompt);
i := Item{}
conn, err := dbus.SessionBus()
if err != nil {
return i, err
}
prop := make(map[string]dbus.Variant)
prop[_ItemLabel] = dbus.MakeVariant(label)
prop[_ItemAttributes] = dbus.MakeVariant(attr)
call := c.Call(_CollectionCreateItem, 0, prop, s, replace)
if call.Err != nil {
return i, call.Err
}
var newItem dbus.ObjectPath
call.Store(&newItem)
i = Item{conn.Object(ServiceName, newItem)}
return i, nil
}
func (c Collection) Locked() bool {
v, err := c.GetProperty(_CollectionLocked)
if err != nil {
panic(err)
}
l := v.Value()
return l.(bool)
}
func (c Collection) Created() time.Time {
v, err := c.GetProperty(_CollectionCreated)
if err != nil {
panic(err)
}
return time.Unix(int64(v.Value().(uint64)), 0).UTC()
}
func (c Collection) Modified() time.Time {
v, err := c.GetProperty(_CollectionModified)
if err != nil {
panic(err)
}
return time.Unix(int64(v.Value().(uint64)), 0).UTC()
}
func (c Collection) Items() []Item {
// How did we get here if we can't get on the bus now?
conn, _ := dbus.SessionBus()
p, _ := c.GetProperty(_CollectionItems)
objs := p.Value().([]dbus.ObjectPath)
i := make([]Item, len(objs))
for idx, objPath := range objs {
i[idx] = Item{conn.Object(ServiceName, objPath)}
}
return i
}
func (c Collection) GetLabel() string {
v, err := c.GetProperty(_CollectionLabel)
if err != nil {
panic(err)
}
return v.Value().(string)
}
func (c Collection) SetLabel(l string) error {
return c.Call(setProp, 0, _Collection, "Label", l).Err
}
func (c Collection) Unlock() error {
conn, _ := dbus.SessionBus()
var prompt dbus.ObjectPath
u := make([]dbus.ObjectPath, 1)
srv := conn.Object(ServiceName, ServicePath)
call := srv.Call(_ServiceUnlock, 0, []dbus.ObjectPath{c.Path()})
if call.Err != nil {
return call.Err
}
if err := call.Store(&u, &prompt); err != nil {
return err
}
if _, err := checkPrompt(prompt); err != nil {
return err
}
return nil
}
type Session struct {
*dbus.Object
Algorithm string
Key []byte
}
// Yes, really, it's the only method that exists on a Session.
func (s Session) Close() {
// spec: Close(void);
s.Go(_SessionClose, dbus.FlagNoReplyExpected, nil)
}
func (s Session) NewSecret() Secret {
r := Secret{s.Path(), nil, nil, text_plain}
switch s.Algorithm {
case AlgoDH:
r.Parameters = make([]byte, aes.BlockSize)
io.ReadFull(rand.Reader, r.Parameters)
}
return r
}