forked from keybase/client
/
saltpack_verify.go
107 lines (88 loc) · 2.7 KB
/
saltpack_verify.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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package engine
import (
"io"
"golang.org/x/net/context"
"github.com/keybase/client/go/libkb"
keybase1 "github.com/keybase/client/go/protocol/keybase1"
"github.com/keybase/saltpack"
)
// SaltpackVerify is an engine.
type SaltpackVerify struct {
libkb.Contextified
arg *SaltpackVerifyArg
key libkb.NaclSigningKeyPair
}
// SaltpackVerifyArg are engine args.
type SaltpackVerifyArg struct {
Sink io.WriteCloser
Source io.Reader
Opts keybase1.SaltpackVerifyOptions
}
// NewSaltpackVerify creates a SaltpackVerify engine.
func NewSaltpackVerify(arg *SaltpackVerifyArg, g *libkb.GlobalContext) *SaltpackVerify {
return &SaltpackVerify{
arg: arg,
Contextified: libkb.NewContextified(g),
}
}
// Name is the unique engine name.
func (e *SaltpackVerify) Name() string {
return "SaltpackVerify"
}
// Prereqs returns the engine prereqs.
func (e *SaltpackVerify) Prereqs() Prereqs {
return Prereqs{}
}
// RequiredUIs returns the required UIs.
func (e *SaltpackVerify) RequiredUIs() []libkb.UIKind {
return []libkb.UIKind{
libkb.SaltpackUIKind,
}
}
// SubConsumers returns the other UI consumers for this engine.
func (e *SaltpackVerify) SubConsumers() []libkb.UIConsumer {
return []libkb.UIConsumer{&SaltpackSenderIdentify{}}
}
// Run starts the engine.
func (e *SaltpackVerify) Run(ctx *Context) error {
if len(e.arg.Opts.Signature) > 0 {
return e.detached(ctx)
}
return e.attached(ctx)
}
func (e *SaltpackVerify) attached(ctx *Context) error {
hook := func(key saltpack.SigningPublicKey) error {
return e.identifySender(ctx, key)
}
return libkb.SaltpackVerify(e.G(), e.arg.Source, e.arg.Sink, hook)
}
func (e *SaltpackVerify) detached(ctx *Context) error {
hook := func(key saltpack.SigningPublicKey) error {
return e.identifySender(ctx, key)
}
return libkb.SaltpackVerifyDetached(e.G(), e.arg.Source, e.arg.Opts.Signature, hook)
}
func (e *SaltpackVerify) identifySender(ctx *Context, key saltpack.SigningPublicKey) (err error) {
defer e.G().Trace("SaltpackVerify::identifySender", func() error { return err })()
kid := libkb.SigningPublicKeyToKeybaseKID(key)
spsiArg := SaltpackSenderIdentifyArg{
publicKey: kid,
reason: keybase1.IdentifyReason{
Reason: "Identify who signed this message",
Type: keybase1.IdentifyReasonType_VERIFY,
},
userAssertion: e.arg.Opts.SignedBy,
}
spsiEng := NewSaltpackSenderIdentify(e.G(), &spsiArg)
if err = RunEngine(spsiEng, ctx); err != nil {
return err
}
arg := keybase1.SaltpackVerifySuccessArg{
Sender: spsiEng.Result(),
SigningKID: kid,
}
ctx.SaltpackUI.SaltpackVerifySuccess(context.TODO(), arg)
return nil
}