forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgp_decrypt.go
145 lines (121 loc) · 3.33 KB
/
pgp_decrypt.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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package engine
import (
"fmt"
"io"
"github.com/keybase/client/go/libkb"
)
type PGPDecryptArg struct {
Source io.Reader
Sink io.WriteCloser
AssertSigned bool
SignedBy string
}
// PGPDecrypt decrypts data read from source into sink for the
// logged in user.
type PGPDecrypt struct {
libkb.Contextified
arg *PGPDecryptArg
signStatus *libkb.SignatureStatus
owner *libkb.User
}
// NewPGPDecrypt creates a PGPDecrypt engine.
func NewPGPDecrypt(arg *PGPDecryptArg, g *libkb.GlobalContext) *PGPDecrypt {
return &PGPDecrypt{
arg: arg,
Contextified: libkb.NewContextified(g),
}
}
// Name is the unique engine name.
func (e *PGPDecrypt) Name() string {
return "PGPDecrypt"
}
// GetPrereqs returns the engine prereqs.
func (e *PGPDecrypt) Prereqs() Prereqs {
return Prereqs{}
}
// RequiredUIs returns the required UIs.
func (e *PGPDecrypt) RequiredUIs() []libkb.UIKind {
return []libkb.UIKind{libkb.SecretUIKind, libkb.LogUIKind, libkb.PgpUIKind}
}
// SubConsumers returns the other UI consumers for this engine.
func (e *PGPDecrypt) SubConsumers() []libkb.UIConsumer {
return []libkb.UIConsumer{
&ScanKeys{},
&Identify{},
}
}
// Run starts the engine.
func (e *PGPDecrypt) Run(ctx *Context) (err error) {
e.G().Log.Debug("+ PGPDecrypt::Run")
defer func() {
e.G().Log.Debug("- PGPDecrypt::Run %s", err)
}()
e.G().Log.Debug("| ScanKeys")
sk, err := NewScanKeys(ctx.SecretUI, e.G())
if err != nil {
return err
}
e.G().Log.Debug("| PGPDecrypt")
e.signStatus, err = libkb.PGPDecrypt(e.arg.Source, e.arg.Sink, sk)
if err != nil {
return err
}
e.G().Log.Debug("| Sink Close")
if err = e.arg.Sink.Close(); err != nil {
return err
}
e.owner = sk.Owner()
if len(e.arg.SignedBy) > 0 {
e.arg.AssertSigned = true
}
if !e.signStatus.IsSigned {
if !e.arg.AssertSigned {
return nil
}
return libkb.BadSigError{E: "no signature in message"}
}
if !e.signStatus.Verified {
return e.signStatus.SignatureError
}
// message is signed and verified
if len(e.arg.SignedBy) > 0 {
// identify the SignedBy assertion
arg := NewIdentifyArg(e.arg.SignedBy, false, false)
eng := NewIdentify(arg, e.G())
if err := RunEngine(eng, ctx); err != nil {
return err
}
signByUser := eng.User()
if signByUser == nil {
// this shouldn't happen (engine should return an error in this state)
// but just in case:
return libkb.ErrNilUser
}
if !signByUser.Equal(e.owner) {
return libkb.BadSigError{
E: fmt.Sprintf("Signer %q did not match signed by assertion %q", e.owner.GetName(), e.arg.SignedBy),
}
}
} else {
// identify the signer
arg := NewIdentifyArg(e.owner.GetName(), false, false)
eng := NewIdentify(arg, e.G())
if err := RunEngine(eng, ctx); err != nil {
return err
}
}
if e.signStatus.Entity == nil {
return libkb.NoKeyError{Msg: fmt.Sprintf("In signature verification: no public key found for PGP ID %x", e.signStatus.KeyID)}
}
bundle := libkb.NewPGPKeyBundle(e.signStatus.Entity)
OutputSignatureSuccess(ctx, bundle.GetFingerprint(), e.owner, e.signStatus.SignatureTime)
return nil
}
func (e *PGPDecrypt) SignatureStatus() *libkb.SignatureStatus {
return e.signStatus
}
func (e *PGPDecrypt) Owner() *libkb.User {
return e.owner
}