/
saltpack_encrypt.go
204 lines (174 loc) · 6.09 KB
/
saltpack_encrypt.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
// 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"
keybase1 "github.com/keybase/client/go/protocol/keybase1"
"github.com/keybase/saltpack"
)
type SaltpackEncryptArg struct {
Opts keybase1.SaltpackEncryptOptions
Source io.Reader
Sink io.WriteCloser
}
// SaltpackEncrypt encrypts data read from a source into a sink
// for a set of users. It will track them if necessary.
type SaltpackEncrypt struct {
arg *SaltpackEncryptArg
me keybase1.UID
newKeyfinderHook (func(arg libkb.SaltpackRecipientKeyfinderArg) libkb.SaltpackRecipientKeyfinderEngineInterface)
// keep track if an SBS recipient was used so callers can tell the user
UsedSBS bool
SBSAssertion string
// Legacy encryption-only messages include a lot more information about
// receivers, and it's nice to keep the helpful errors working while those
// messages are still around.
visibleRecipientsForTesting bool
}
// NewSaltpackEncrypt creates a SaltpackEncrypt engine.
func NewSaltpackEncrypt(arg *SaltpackEncryptArg, newKeyfinderHook func(arg libkb.SaltpackRecipientKeyfinderArg) libkb.SaltpackRecipientKeyfinderEngineInterface) *SaltpackEncrypt {
return &SaltpackEncrypt{
arg: arg,
newKeyfinderHook: newKeyfinderHook,
}
}
// Name is the unique engine name.
func (e *SaltpackEncrypt) Name() string {
return "SaltpackEncrypt"
}
// Prereqs returns the engine prereqs.
func (e *SaltpackEncrypt) Prereqs() Prereqs {
return Prereqs{}
}
// RequiredUIs returns the required UIs.
func (e *SaltpackEncrypt) RequiredUIs() []libkb.UIKind {
return []libkb.UIKind{
libkb.SecretUIKind,
}
}
// SubConsumers returns the other UI consumers for this engine.
func (e *SaltpackEncrypt) SubConsumers() []libkb.UIConsumer {
// Note that potentially KeyfinderHook might return a different UIConsumer depending on its arguments,
// which might make this call problematic, but all the hooks currently in use are not doing that.
return []libkb.UIConsumer{
e.newKeyfinderHook(libkb.SaltpackRecipientKeyfinderArg{}),
}
}
func (e *SaltpackEncrypt) loadMe(m libkb.MetaContext) error {
loggedIn, uid, err := isLoggedInWithUIDAndError(m)
if err != nil && !e.arg.Opts.NoSelfEncrypt {
return err
}
if !loggedIn {
return nil
}
e.me = uid
return nil
}
// Run starts the engine.
func (e *SaltpackEncrypt) Run(m libkb.MetaContext) (err error) {
defer m.Trace("SaltpackEncrypt::Run", &err)()
if err = e.loadMe(m); err != nil {
return err
}
if !(e.arg.Opts.UseEntityKeys || e.arg.Opts.UseDeviceKeys || e.arg.Opts.UsePaperKeys || e.arg.Opts.UseKBFSKeysOnlyForTesting) {
return fmt.Errorf("no key type for encryption was specified")
}
kfarg := libkb.SaltpackRecipientKeyfinderArg{
Recipients: e.arg.Opts.Recipients,
TeamRecipients: e.arg.Opts.TeamRecipients,
NoSelfEncrypt: e.arg.Opts.NoSelfEncrypt,
UseEntityKeys: e.arg.Opts.UseEntityKeys,
UsePaperKeys: e.arg.Opts.UsePaperKeys,
UseDeviceKeys: e.arg.Opts.UseDeviceKeys,
UseRepudiableAuth: e.arg.Opts.AuthenticityType == keybase1.AuthenticityType_REPUDIABLE,
NoForcePoll: e.arg.Opts.NoForcePoll,
}
kf := e.newKeyfinderHook(kfarg)
if err := RunEngine2(m, kf); err != nil {
return err
}
var receivers []libkb.NaclDHKeyPublic
for _, KID := range kf.GetPublicKIDs() {
gk, err := libkb.ImportKeypairFromKID(KID)
if err != nil {
return err
}
kp, ok := gk.(libkb.NaclDHKeyPair)
if !ok {
return libkb.KeyCannotEncryptError{}
}
receivers = append(receivers, kp.Public)
}
var symmetricReceivers []saltpack.ReceiverSymmetricKey
for _, key := range kf.GetSymmetricKeys() {
symmetricReceivers = append(symmetricReceivers, saltpack.ReceiverSymmetricKey{
Key: saltpack.SymmetricKey(key.Key),
Identifier: key.Identifier,
})
}
e.UsedSBS, e.SBSAssertion = kf.UsedUnresolvedSBSAssertion()
if e.UsedSBS {
actx := m.G().MakeAssertionContext(m)
expr, err := libkb.AssertionParse(actx, e.SBSAssertion)
if err == nil {
social, err := expr.ToSocialAssertion()
if err == nil && social.Service == "email" {
// email assertions are pretty ugly, so just return
// the "User" part for easier handling upstream.
e.SBSAssertion = social.User
}
}
}
// This flag determines whether saltpack is used in signcryption (false)
// vs encryption (true) format.
encryptionOnlyMode := false
var senderDH libkb.NaclDHKeyPair
if e.arg.Opts.AuthenticityType == keybase1.AuthenticityType_REPUDIABLE && !e.me.IsNil() {
encryptionOnlyMode = true
dhKey, err := m.G().ActiveDevice.EncryptionKeyWithUID(e.me)
if err != nil {
return err
}
dhKeypair, ok := dhKey.(libkb.NaclDHKeyPair)
if !ok || dhKeypair.Private == nil {
return libkb.KeyCannotEncryptError{}
}
senderDH = dhKeypair
}
var senderSigning libkb.NaclSigningKeyPair
if e.arg.Opts.AuthenticityType == keybase1.AuthenticityType_SIGNED && !e.me.IsNil() {
signingKey, err := m.G().ActiveDevice.SigningKeyWithUID(e.me)
if err != nil {
return err
}
signingKeypair, ok := signingKey.(libkb.NaclSigningKeyPair)
if !ok || signingKeypair.Private == nil {
// Perhaps a KeyCannotEncrypt error, although less accurate, would be more intuitive for the user.
return libkb.KeyCannotSignError{}
}
senderSigning = signingKeypair
}
if e.arg.Opts.AuthenticityType != keybase1.AuthenticityType_ANONYMOUS && e.me.IsNil() {
return libkb.NewLoginRequiredError("authenticating a message requires login. Either login or use --auth-type=anonymous")
}
saltpackVersion, err := libkb.SaltpackVersionFromArg(e.arg.Opts.SaltpackVersion)
if err != nil {
return err
}
encarg := libkb.SaltpackEncryptArg{
Source: e.arg.Source,
Sink: e.arg.Sink,
Receivers: receivers,
Sender: senderDH,
SenderSigning: senderSigning,
Binary: e.arg.Opts.Binary,
EncryptionOnlyMode: encryptionOnlyMode,
SymmetricReceivers: symmetricReceivers,
SaltpackVersion: saltpackVersion,
VisibleRecipientsForTesting: e.visibleRecipientsForTesting,
}
return libkb.SaltpackEncrypt(m, &encarg)
}