forked from keybase/client
/
cmd_pgp_decrypt.go
108 lines (99 loc) · 2.75 KB
/
cmd_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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package client
import (
"github.com/keybase/cli"
"github.com/keybase/client/go/libcmdline"
"github.com/keybase/client/go/libkb"
keybase1 "github.com/keybase/client/go/protocol/keybase1"
"github.com/keybase/go-framed-msgpack-rpc/rpc"
"golang.org/x/net/context"
)
func NewCmdPGPDecrypt(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
return cli.Command{
Name: "decrypt",
Usage: "PGP decrypt messages or files for keybase users",
Action: func(c *cli.Context) {
cl.ChooseCommand(&CmdPGPDecrypt{Contextified: libkb.NewContextified(g)}, "decrypt", c)
},
Flags: []cli.Flag{
cli.StringFlag{
Name: "i, infile",
Usage: "Specify an input file.",
},
cli.StringFlag{
Name: "m, message",
Usage: "Provide the message on the command line.",
},
cli.StringFlag{
Name: "o, outfile",
Usage: "Specify an outfile (stdout by default).",
},
cli.BoolFlag{
Name: "s, signed",
Usage: "Assert signed.",
},
cli.StringFlag{
Name: "S, signed-by",
Usage: "Assert signed by the given user (can use user assertion format).",
},
},
Description: `Use of this command requires at least one PGP secret key imported
into the local Keybase keyring. It will try all secret keys in the local keyring that match the
given ciphertext, and will succeed so long as one such key is available.`,
}
}
type CmdPGPDecrypt struct {
libkb.Contextified
UnixFilter
signed bool
signedBy string
}
func (c *CmdPGPDecrypt) Run() error {
cli, err := GetPGPClient(c.G())
if err != nil {
return err
}
protocols := []rpc.Protocol{
NewStreamUIProtocol(c.G()),
NewSecretUIProtocol(c.G()),
NewPgpUIProtocol(c.G()),
NewIdentifyUIProtocol(c.G()),
}
if err := RegisterProtocolsWithContext(protocols, c.G()); err != nil {
return err
}
snk, src, err := c.ClientFilterOpen(c.G())
if err != nil {
return err
}
opts := keybase1.PGPDecryptOptions{
AssertSigned: c.signed,
SignedBy: c.signedBy,
}
arg := keybase1.PGPDecryptArg{Source: src, Sink: snk, Opts: opts}
_, err = cli.PGPDecrypt(context.TODO(), arg)
cerr := c.Close(err)
return libkb.PickFirstError(err, cerr)
}
func (c *CmdPGPDecrypt) ParseArgv(ctx *cli.Context) error {
if len(ctx.Args()) > 0 {
return UnexpectedArgsError("pgp decrypt")
}
msg := ctx.String("message")
outfile := ctx.String("outfile")
infile := ctx.String("infile")
if err := c.FilterInit(c.G(), msg, infile, outfile); err != nil {
return err
}
c.signed = ctx.Bool("signed")
c.signedBy = ctx.String("signed-by")
return nil
}
func (c *CmdPGPDecrypt) GetUsage() libkb.Usage {
return libkb.Usage{
Config: true,
API: true,
KbKeyring: true,
}
}