-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmd_pgp_encrypt.go
145 lines (135 loc) · 3.59 KB
/
cmd_pgp_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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package client
import (
"errors"
"golang.org/x/net/context"
"github.com/keybase/cli"
"github.com/keybase/client/go/libcmdline"
"github.com/keybase/client/go/libkb"
keybase1 "github.com/keybase/client/go/protocol"
rpc "github.com/keybase/go-framed-msgpack-rpc"
)
func NewCmdPGPEncrypt(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
return cli.Command{
Name: "encrypt",
ArgumentHelp: "<usernames...>",
Usage: "PGP encrypt messages or files for keybase users",
Action: func(c *cli.Context) {
cl.ChooseCommand(&CmdPGPEncrypt{Contextified: libkb.NewContextified(g)}, "encrypt", c)
},
Flags: []cli.Flag{
cli.BoolFlag{
Name: "b, binary",
Usage: "Output in binary (rather than ASCII/armored).",
},
cli.StringFlag{
Name: "i, infile",
Usage: "Specify an input file.",
},
cli.StringFlag{
Name: "k, key",
Usage: "Specify a key to use (otherwise most recent PGP key is used).",
},
cli.BoolFlag{
Name: "l, local",
Usage: "Only track locally, don't send a statement to the server.",
},
cli.StringFlag{
Name: "m, message",
Usage: "Provide the message on the command line.",
},
cli.BoolFlag{
Name: "no-self",
Usage: "Don't encrypt for self.",
},
cli.StringFlag{
Name: "o, outfile",
Usage: "Specify an outfile (stdout by default).",
},
cli.BoolFlag{
Name: "s, sign",
Usage: "Sign in addition to encrypting.",
},
cli.BoolFlag{
Name: "skip-track",
Usage: "Don't track.",
},
cli.BoolFlag{
Name: "y",
Usage: "Approve remote tracking without prompting.",
},
},
Description: `If encrypting with signatures, "keybase pgp encrypt" requires an
imported PGP private key, and accesses the local Keybase keyring when producing
the signature.`,
}
}
type CmdPGPEncrypt struct {
libkb.Contextified
UnixFilter
recipients []string
trackOptions keybase1.TrackOptions
sign bool
noSelf bool
keyQuery string
binaryOut bool
}
func (c *CmdPGPEncrypt) Run() error {
cli, err := GetPGPClient()
if err != nil {
return err
}
protocols := []rpc.Protocol{
NewStreamUIProtocol(),
NewSecretUIProtocol(c.G()),
NewIdentifyTrackUIProtocol(c.G()),
}
if err := RegisterProtocols(protocols); err != nil {
return err
}
snk, src, err := c.ClientFilterOpen()
if err != nil {
return err
}
opts := keybase1.PGPEncryptOptions{
Recipients: c.recipients,
NoSign: !c.sign,
NoSelf: c.noSelf,
BinaryOut: c.binaryOut,
KeyQuery: c.keyQuery,
TrackOptions: c.trackOptions,
}
arg := keybase1.PGPEncryptArg{Source: src, Sink: snk, Opts: opts}
err = cli.PGPEncrypt(context.TODO(), arg)
cerr := c.Close(err)
return libkb.PickFirstError(err, cerr)
}
func (c *CmdPGPEncrypt) ParseArgv(ctx *cli.Context) error {
c.noSelf = ctx.Bool("no-self")
if c.noSelf && len(ctx.Args()) == 0 {
return errors.New("Encrypt needs at least one recipient, or --no-self=false")
}
msg := ctx.String("message")
outfile := ctx.String("outfile")
infile := ctx.String("infile")
if err := c.FilterInit(msg, infile, outfile); err != nil {
return err
}
c.recipients = ctx.Args()
c.trackOptions = keybase1.TrackOptions{
LocalOnly: ctx.Bool("local"),
BypassConfirm: ctx.Bool("y"),
}
c.sign = ctx.Bool("sign")
c.keyQuery = ctx.String("key")
c.binaryOut = ctx.Bool("binary")
return nil
}
func (c *CmdPGPEncrypt) GetUsage() libkb.Usage {
return libkb.Usage{
Config: true,
API: true,
KbKeyring: true,
}
}