forked from keybase/client
/
cmd_pgp_gen.go
215 lines (189 loc) · 6.14 KB
/
cmd_pgp_gen.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
205
206
207
208
209
210
211
212
213
214
215
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package client
import (
"fmt"
"golang.org/x/net/context"
"github.com/keybase/cli"
"github.com/keybase/client/go/engine"
"github.com/keybase/client/go/libcmdline"
"github.com/keybase/client/go/libkb"
"github.com/keybase/go-framed-msgpack-rpc/rpc"
)
type CmdPGPGen struct {
libkb.Contextified
arg engine.PGPKeyImportEngineArg
}
var SmallKey = 1024
func (v *CmdPGPGen) ParseArgv(ctx *cli.Context) (err error) {
nargs := len(ctx.Args())
if nargs != 0 {
err = fmt.Errorf("pgp gen takes 0 args")
} else {
g := libkb.PGPGenArg{}
g.PGPUids = ctx.StringSlice("pgp-uid")
v.arg.DoExport = !ctx.Bool("no-export")
v.arg.AllowMulti = ctx.Bool("multi")
if ctx.Bool("debug") {
g.PrimaryBits = SmallKey
g.SubkeyBits = SmallKey
}
v.arg.Gen = &g
}
return err
}
// Why use CreatePGPIDs rather than MakeAllIds?
func (v *CmdPGPGen) Run() (err error) {
protocols := []rpc.Protocol{
NewSecretUIProtocol(v.G()),
}
cli, err := GetPGPClient(v.G())
if err != nil {
return err
}
if err = RegisterProtocolsWithContext(protocols, v.G()); err != nil {
return err
}
// Prompt for user IDs if none given on command line
if len(v.arg.Gen.PGPUids) == 0 {
if err = v.propmptPGPIDs(); err != nil {
return err
}
} else if err = v.arg.Gen.CreatePGPIDs(); err != nil {
return err
}
v.arg.PushSecret, err = v.G().UI.GetTerminalUI().PromptYesNo(PromptDescriptorPGPGenPushSecret, "Push an encrypted copy of your new secret key to the Keybase.io server?", libkb.PromptDefaultYes)
if err != nil {
return err
}
if v.arg.DoExport {
v.arg.ExportEncrypted, err = v.G().UI.GetTerminalUI().PromptYesNo(PromptDescriptorPGPGenEncryptSecret, "When exporting to the GnuPG keychain, encrypt private keys with a passphrase?", libkb.PromptDefaultYes)
if err != nil {
return err
}
}
err = cli.PGPKeyGen(context.TODO(), v.arg.Export())
err = AddPGPMultiInstructions(err)
return err
}
var CheckRealName = libkb.Checker{
F: func(s string) bool {
nameID, err := libkb.ParseIdentity(s)
if err != nil {
return false
}
return len(nameID.Username) > 0 && len(nameID.Comment) == 0 && len(nameID.Email) == 0
},
Hint: "for example: \"Ned Snowben\"",
}
var CheckOptionalEmail = libkb.Checker{
F: func(s string) bool {
if len(s) == 0 {
return true
}
return libkb.CheckEmail.F(s)
},
Hint: libkb.CheckEmail.Hint,
}
func (v *CmdPGPGen) propmptPGPIDs() (err error) {
id := libkb.Identity{}
prompt := "Enter your real name, which will be publicly visible in your new key"
id.Username, err = PromptWithChecker(PromptDescriptorPGPGenEnterID, v.G().UI.GetTerminalUI(), prompt, false, CheckRealName)
if err != nil {
return
}
// Email required for primary ID
prompt = "Enter a public email address for your key"
id.Email, err = PromptWithChecker(PromptDescriptorPGPGenEnterID, v.G().UI.GetTerminalUI(), prompt, false, libkb.CheckEmail)
if err != nil {
return
}
v.arg.Gen.Ids = append(v.arg.Gen.Ids, id)
emailsSeen := make(map[string]struct{})
emailsSeen[id.Email] = struct{}{}
idAdditional := libkb.Identity{
Username: id.Username,
}
prompt = "Enter another email address (or <enter> when done)"
for {
idAdditional.Email, err = PromptWithChecker(PromptDescriptorPGPGenEnterID, v.G().UI.GetTerminalUI(), prompt, false, CheckOptionalEmail)
if err != nil || len(idAdditional.Email) == 0 {
break
}
// Make sure it hasn't been added already
if _, ok := emailsSeen[idAdditional.Email]; ok {
v.G().Log.Warning("Email already applied to this key")
continue
}
emailsSeen[idAdditional.Email] = struct{}{}
v.arg.Gen.Ids = append(v.arg.Gen.Ids, idAdditional)
}
return
}
func AddPGPMultiInstructions(err error) error {
if err == nil {
return nil
}
if kee, ok := err.(libkb.KeyExistsError); ok {
return fmt.Errorf("You already have a PGP key registered (%s)\n"+
"Specify the `--multi` flag to override this check",
kee.Key.ToQuads())
}
// Not the right type. Return it as is.
return err
}
func NewCmdPGPGen(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
return cli.Command{
Name: "gen",
Usage: "Generate a new PGP key and write to local secret keychain",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "d, debug",
Usage: "Generate small keys for debugging.",
},
cli.StringSliceFlag{
Name: "pgp-uid",
Usage: "Specify custom PGP uid(s).",
Value: &cli.StringSlice{},
},
cli.BoolFlag{
Name: "multi",
Usage: "Allow multiple PGP keys.",
},
cli.BoolFlag{
Name: "no-export",
Usage: "Disable exporting of new keys to GPG keychain.",
},
},
Description: `"keybase pgp gen" generates a new PGP key for this account.
In all cases, it signs the public key with an exising device key,
and pushes the signature to the server. Thus, the user will have a
publicly-visible "PGP device" after running this operation.
The secret half of the PGP key is written by default to the user's
local Keybase keychain and encrypted with the "local key security"
(LKS) protocol. (For more information, try 'keybase help keyring').
Also, by default, the public **and secret** halves of the new PGP key
are exported to the local GnuPG keyring, if one is found. Unless the
"--unencrypted" flag is provided, you will be asked to provide a
passphrase to encrypt the key in the GnuPG keyring. You can specify
"--no-export" to stop the export of the newly generated key to the
GnuPG keyring.
On subsequent secret key accesses --- say for PGP decryption or
for signing --- access to the local GnuPG keyring is not required.
Rather, keybase will access the secret PGP key in its own local keychain.
By default, the secret half of the PGP key is never exported off
of the local system, but users have a choice via terminal prompt
to select storage of their encrypted secret PGP key on the Keybase
servers.`,
Action: func(c *cli.Context) {
cl.ChooseCommand(&CmdPGPGen{Contextified: libkb.NewContextified(g)}, "gen", c)
},
}
}
func (v *CmdPGPGen) GetUsage() libkb.Usage {
return libkb.Usage{
Config: true,
KbKeyring: true,
API: true,
}
}