forked from keybase/client
/
pgp_decrypt.go
167 lines (141 loc) · 4.13 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// 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"
"github.com/keybase/client/go/protocol/keybase1"
)
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
signer *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{},
&ResolveThenIdentify2{},
}
}
// Run starts the engine.
func (e *PGPDecrypt) Run(ctx *Context) (err error) {
defer e.G().Trace("PGPDecrypt::Run", func() error { return 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.G(), 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
}
// get the owner of the signing key
e.signer = sk.KeyOwner(e.signStatus.KeyID)
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 {
if e.signer == nil {
return libkb.BadSigError{
E: fmt.Sprintf("Signer not a keybase user, cannot match signed by assertion %q", e.arg.SignedBy),
}
}
// identify the SignedBy assertion
arg := keybase1.Identify2Arg{
UserAssertion: e.arg.SignedBy,
AlwaysBlock: true,
NeedProofSet: true,
NoSkipSelf: true,
}
eng := NewResolveThenIdentify2(e.G(), &arg)
if err := RunEngine(eng, ctx); err != nil {
return err
}
signByUser := eng.Result().Upk
if !signByUser.Uid.Equal(e.signer.GetUID()) {
return libkb.BadSigError{
E: fmt.Sprintf("Signer %q did not match signed by assertion %q", e.signer.GetName(), e.arg.SignedBy),
}
}
} else {
if e.signer == nil {
// signer isn't a keybase user
e.G().Log.Debug("message signed by key unknown to keybase: %X", e.signStatus.KeyID)
OutputSignatureSuccessNonKeybase(ctx, e.signStatus.KeyID, e.signStatus.SignatureTime)
return nil
}
// identify the signer
arg := keybase1.Identify2Arg{
UserAssertion: e.signer.GetName(),
AlwaysBlock: true,
NeedProofSet: true,
NoSkipSelf: true,
}
eng := NewResolveThenIdentify2(e.G(), &arg)
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)}
}
if entity := e.signStatus.Entity; len(entity.UnverifiedRevocations) > 0 {
return libkb.BadSigError{
E: fmt.Sprintf("Key %x belonging to %q has been revoked by its designated revoker.", entity.PrimaryKey.KeyId, e.signer.GetName()),
}
}
bundle := libkb.NewPGPKeyBundle(e.signStatus.Entity)
OutputSignatureSuccess(ctx, bundle.GetFingerprint(), e.signer, e.signStatus.SignatureTime)
return nil
}
func (e *PGPDecrypt) SignatureStatus() *libkb.SignatureStatus {
return e.signStatus
}
func (e *PGPDecrypt) Signer() *libkb.User {
return e.signer
}