forked from keybase/client
/
cmd_test_crypto.go
154 lines (136 loc) · 3.85 KB
/
cmd_test_crypto.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
package client
import (
"errors"
"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"
)
func newCmdTestCrypto(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
return cli.Command{
Name: "test-crypto",
Usage: "Test the frontend saltpack protocol",
Action: func(c *cli.Context) {
cl.ChooseCommand(&CmdTestCrypto{Contextified: libkb.NewContextified(g)}, "test-crypto", c)
},
Flags: []cli.Flag{
cli.StringFlag{
Name: "f",
Usage: "filename to encrypt",
},
},
}
}
type CmdTestCrypto struct {
libkb.Contextified
recipients []string
filename string
}
func (s *CmdTestCrypto) ParseArgv(ctx *cli.Context) error {
s.recipients = ctx.Args()
if len(s.recipients) == 0 {
return errors.New("need at least one recipient")
}
s.filename = ctx.String("f")
return nil
}
func (s *CmdTestCrypto) GetUsage() libkb.Usage {
return libkb.Usage{}
}
func (s *CmdTestCrypto) Run() (err error) {
mctx := libkb.NewMetaContextBackground(s.G())
cli, err := GetSaltpackClient(s.G())
if err != nil {
return err
}
protocols := []rpc.Protocol{
NewSecretUIProtocol(s.G()),
}
if err := RegisterProtocolsWithContext(protocols, s.G()); err != nil {
return err
}
plaintext := "hello goodbye"
dui := s.G().UI.GetDumbOutputUI()
dui.Printf("encrypting string %q for %v\n", plaintext, s.recipients)
arg := keybase1.SaltpackEncryptStringArg{
Plaintext: plaintext,
Opts: keybase1.SaltpackFrontendEncryptOptions{
Recipients: s.recipients,
IncludeSelf: true,
Signed: true,
},
}
sres, err := cli.SaltpackEncryptString(mctx.Ctx(), arg)
if err != nil {
return err
}
dui.Printf("ciphertext:\n\n")
dui.Printf("%s\n\n", sres.Ciphertext)
dui.Printf("decrypting ciphertext\n")
decArg := keybase1.SaltpackDecryptStringArg{
Ciphertext: sres.Ciphertext,
}
res, err := cli.SaltpackDecryptString(mctx.Ctx(), decArg)
if err != nil {
return err
}
dui.Printf("plaintext: %q\n\n", res.Plaintext)
dui.Printf("info: %+v\n\n", res.Info)
dui.Printf("signed: %v\n", res.Signed)
dui.Printf("signing string %q\n", plaintext)
signArg := keybase1.SaltpackSignStringArg{Plaintext: plaintext}
signed, err := cli.SaltpackSignString(mctx.Ctx(), signArg)
if err != nil {
return err
}
dui.Printf("signed:\n\n")
dui.Printf("%s\n\n", signed)
dui.Printf("verifying signed msg\n")
verifyArg := keybase1.SaltpackVerifyStringArg{SignedMsg: signed}
vres, err := cli.SaltpackVerifyString(mctx.Ctx(), verifyArg)
if err != nil {
return err
}
dui.Printf("verify result: %+v\n\n", vres)
if s.filename == "" {
dui.Printf("no filename specified, done.\n")
return nil
}
dui.Printf("encrypting file %s\n", s.filename)
efArg := keybase1.SaltpackEncryptFileArg{
Filename: s.filename,
Opts: keybase1.SaltpackFrontendEncryptOptions{
Recipients: s.recipients,
IncludeSelf: true,
Signed: true,
},
}
efres, err := cli.SaltpackEncryptFile(mctx.Ctx(), efArg)
if err != nil {
return err
}
dui.Printf("encrypted file result: %+v\n", efres)
dui.Printf("decrypting file: %s\n", efres.Filename)
dfArg := keybase1.SaltpackDecryptFileArg{EncryptedFilename: efres.Filename}
dfres, err := cli.SaltpackDecryptFile(mctx.Ctx(), dfArg)
if err != nil {
return err
}
dui.Printf("decrypt result: %+v\n", dfres)
dui.Printf("signing file %s\n", s.filename)
sfArg := keybase1.SaltpackSignFileArg{Filename: s.filename}
sfPath, err := cli.SaltpackSignFile(mctx.Ctx(), sfArg)
if err != nil {
return err
}
dui.Printf("signed file: %s\n", sfPath)
dui.Printf("verifying file %s\n", sfPath)
vfArg := keybase1.SaltpackVerifyFileArg{SignedFilename: sfPath}
vfres, err := cli.SaltpackVerifyFile(mctx.Ctx(), vfArg)
if err != nil {
return err
}
dui.Printf("verified result: %+v\n", vfres)
return nil
}