Skip to content

Commit 2ed3ab3

Browse files
authored
Prevent user from being able to push secret keys if they have a random password (keybase#16951)
1 parent c049531 commit 2ed3ab3

23 files changed

Lines changed: 200 additions & 95 deletions

File tree

‎go/client/cmd_pgp_gen.go‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/keybase/client/go/engine"
1313
"github.com/keybase/client/go/libcmdline"
1414
"github.com/keybase/client/go/libkb"
15+
"github.com/keybase/client/go/protocol/keybase1"
1516
"github.com/keybase/go-framed-msgpack-rpc/rpc"
1617
)
1718

@@ -49,6 +50,11 @@ func (v *CmdPGPGen) Run() (err error) {
4950
if err != nil {
5051
return err
5152
}
53+
user, err := GetUserClient(v.G())
54+
if err != nil {
55+
return err
56+
}
57+
5258
if err = RegisterProtocolsWithContext(protocols, v.G()); err != nil {
5359
return err
5460
}
@@ -61,10 +67,17 @@ func (v *CmdPGPGen) Run() (err error) {
6167
} else if err = v.arg.Gen.CreatePGPIDs(); err != nil {
6268
return err
6369
}
64-
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)
70+
71+
hasRandomPw, err := user.LoadHasRandomPw(context.TODO(), keybase1.LoadHasRandomPwArg{})
6572
if err != nil {
6673
return err
6774
}
75+
if !hasRandomPw {
76+
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)
77+
if err != nil {
78+
return err
79+
}
80+
}
6881
if v.arg.DoExport {
6982
v.arg.ExportEncrypted, err = v.G().UI.GetTerminalUI().PromptYesNo(PromptDescriptorPGPGenEncryptSecret, "When exporting to the GnuPG keychain, encrypt private keys with a passphrase?", libkb.PromptDefaultYes)
7083
if err != nil {

‎go/client/pgp_ui.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (p PgpUI) KeyGenerated(ctx context.Context, arg keybase1.KeyGeneratedArg) e
6060
return nil
6161
}
6262

63-
func (p PgpUI) ShouldPushPrivate(ctx context.Context, sessionID int) (bool, error) {
63+
func (p PgpUI) ShouldPushPrivate(ctx context.Context, arg keybase1.ShouldPushPrivateArg) (bool, error) {
6464
return false, nil
6565
}
6666

‎go/engine/pgp_decrypt_test.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func TestPGPDecryptArmored(t *testing.T) {
107107

108108
// A saltpack message
109109
saltpack := `BEGIN KEYBASE SALTPACK ENCRYPTED MESSAGE.
110-
kiOUtMhcc4NXXRb XMxIeCbf5rGHq6R lPA7tN3yNXtviMm fQULSIc4k93xPgP ranhF1KYLsndSXH 5hK5WHvPJYOvN3G XLzn8MQ3Q5nwFfY K81cz83VtMGh4CC QnNDeIWVZawH75I jFf8SlDWsdj139W FeS69yb9b7Mo5fA rusOzT3JaydVWgf qC7hU3CUvYR65nP xUpmT2HdHPG5MQu XsOhrf5Zv39u9BB AOkyDAgD7hVSSl9 JQ3eYiduTli4Bqh Ri3uJyBayvkWZTF PSOdMhIbjCptBQ3 oTdvh6pOaUPQeoJ ENL1iuVK04KCOy9 xFekloWTI94hkgt gZakcYbimhmzhea Dsgl2mVqgIwmHgv hp5Indezz4TNtOh VJ8c4BBt1NEzIDg ZfFUiAALL0jRfrB cz7tQc1ussnYzrI IfHSFPDe9Cvz9lp gb1BBogunZOkoNW skfxofDP2lX3Qx7 QP5ah5zm8VV0iw1 zfQaNoxicwkqrM8 tfxyKUWZAypOKoF wUIaC1CQIkTZANa bIJyCxs9g6WceUE jLhh4PazAPCMOU9 M3zOBPP1ieDvc0M OzBLKtBWwz0J1nU 0wtMiADTMKMV.
110+
kiOUtMhcc4NXXRb XMxIeCbf5rGHq6R lPA7tN3yNXtviMm fQULSIc4k93xPgP ranhF1KYLsndSXH 5hK5WHvPJYOvN3G XLzn8MQ3Q5nwFfY K81cz83VtMGh4CC QnNDeIWVZawH75I jFf8SlDWsdj139W FeS69yb9b7Mo5fA rusOzT3JaydVWgf qC7hU3CUvYR65nP xUpmT2HdHPG5MQu XsOhrf5Zv39u9BB AOkyDAgD7hVSSl9 JQ3eYiduTli4Bqh Ri3uJyBayvkWZTF PSOdMhIbjCptBQ3 oTdvh6pOaUPQeoJ ENL1iuVK04KCOy9 xFekloWTI94hkgt gZakcYbimhmzhea Dsgl2mVqgIwmHgv hp5Indezz4TNtOh VJ8c4BBt1NEzIDg ZfFUiAALL0jRfrB cz7tQc1ussnYzrI IfHSFPDe9Cvz9lp gb1BBogunZOkoNW skfxofDP2lX3Qx7 QP5ah5zm8VV0iw1 zfQaNoxicwkqrM8 tfxyKUWZAypOKoF wUIaC1CQIkTZANa bIJyCxs9g6WceUE jLhh4PazAPCMOU9 M3zOBPP1ieDvc0M OzBLKtBWwz0J1nU 0wtMiADTMKMV.
111111
END KEYBASE SALTPACK ENCRYPTED MESSAGE.`
112112

113113
decoded = libkb.NewBufferCloser()
@@ -492,7 +492,7 @@ func (t *TestPgpUI) KeyGenerated(ctx context.Context, arg keybase1.KeyGeneratedA
492492
return nil
493493
}
494494

495-
func (t *TestPgpUI) ShouldPushPrivate(context.Context, int) (bool, error) {
495+
func (t *TestPgpUI) ShouldPushPrivate(context.Context, keybase1.ShouldPushPrivateArg) (bool, error) {
496496
return t.ShouldPush, nil
497497
}
498498

‎go/engine/pgp_import_key.go‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,12 @@ func (e *PGPKeyImportEngine) Run(m libkb.MetaContext) (err error) {
188188
return err
189189
}
190190

191+
if e.arg.PushSecret {
192+
if err = e.checkRandomPassword(m); err != nil {
193+
return err
194+
}
195+
}
196+
191197
if err = e.checkPregenPrivate(); err != nil {
192198
return err
193199
}
@@ -234,6 +240,17 @@ func (e *PGPKeyImportEngine) Run(m libkb.MetaContext) (err error) {
234240
return nil
235241
}
236242

243+
func (e *PGPKeyImportEngine) checkRandomPassword(mctx libkb.MetaContext) error {
244+
random, err := libkb.LoadHasRandomPw(mctx, keybase1.LoadHasRandomPwArg{})
245+
if err != nil {
246+
return err
247+
}
248+
if random {
249+
return libkb.NewPushSecretWithoutPasswordError("You need to set your password first before uploading secret keys")
250+
}
251+
return nil
252+
}
253+
237254
// clonePGPKeyBundle returns an approximate deep copy of PGPKeyBundle
238255
// by exporting and re-importing PGPKeyBundle. If PGP key contains
239256
// something that is not supported by either go-crypto exporter or

‎go/engine/pgp_import_key_test.go‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,31 @@ func TestPGPImportGPGExport(t *testing.T) {
316316
}
317317
}
318318

319+
// TestPGPImportPushSecretWithoutPassword - the engine should prevent nopw
320+
// users from importing secret keys
321+
func TestPGPImportPushSecretWithoutPassword(t *testing.T) {
322+
tc := SetupEngineTest(t, "pgpnopw")
323+
defer tc.Cleanup()
324+
325+
user, _ := NewFakeUser("login")
326+
arg := MakeTestSignupEngineRunArg(user)
327+
arg.StoreSecret = true
328+
arg.GenerateRandomPassphrase = true
329+
arg.Passphrase = ""
330+
_, err := CreateAndSignupFakeUserSafeWithArg(tc.G, user, arg)
331+
require.NoError(t, err)
332+
333+
secui := &libkb.TestSecretUI{}
334+
uis := libkb.UIs{LogUI: tc.G.UI.GetLogUI(), SecretUI: secui}
335+
_, _, key := armorKey(t, tc, user.Email)
336+
eng, err := NewPGPKeyImportEngineFromBytes(tc.G, []byte(key), true)
337+
require.Nil(t, err, "engine initialization should succeed")
338+
m := NewMetaContextForTest(tc).WithUIs(uis)
339+
err = RunEngine2(m, eng)
340+
require.NotNil(t, err, "import should fail")
341+
require.Contains(t, err.Error(), "You need to set your password first before uploading secret keys")
342+
}
343+
319344
func numPrivateGPGKeys(g *libkb.GlobalContext) (int, error) {
320345
gpg := g.GetGpgClient()
321346
if err := gpg.Configure(); err != nil {

‎go/engine/pgp_keygen.go‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,15 @@ func (e *PGPKeyGen) Run(m libkb.MetaContext) error {
8787
return err
8888
}
8989

90-
// ask if we should push private key to api server
91-
pushPrivate, err := m.UIs().PgpUI.ShouldPushPrivate(m.Ctx(), m.UIs().SessionID)
90+
// ask if we should push private key to api server if user has a password
91+
hasRandomPw, err := libkb.LoadHasRandomPw(m, keybase1.LoadHasRandomPwArg{})
92+
if err != nil {
93+
return err
94+
}
95+
pushPrivate, err := m.UIs().PgpUI.ShouldPushPrivate(m.Ctx(), keybase1.ShouldPushPrivateArg{
96+
SessionID: m.UIs().SessionID,
97+
Prompt: !hasRandomPw,
98+
})
9299
if err != nil {
93100
return err
94101
}

‎go/libkb/errors.go‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2660,6 +2660,8 @@ func (e ResetMissingParamsError) Error() string {
26602660
return e.msg
26612661
}
26622662

2663+
//============================================================================
2664+
26632665
type ChainLinkBadUnstubError struct {
26642666
msg string
26652667
}
@@ -2671,3 +2673,17 @@ func NewChainLinkBadUnstubError(s string) error {
26712673
func (c ChainLinkBadUnstubError) Error() string {
26722674
return c.msg
26732675
}
2676+
2677+
//============================================================================
2678+
2679+
type PushSecretWithoutPasswordError struct {
2680+
msg string
2681+
}
2682+
2683+
func NewPushSecretWithoutPasswordError(msg string) error {
2684+
return PushSecretWithoutPasswordError{msg: msg}
2685+
}
2686+
2687+
func (e PushSecretWithoutPasswordError) Error() string {
2688+
return e.msg
2689+
}

‎go/libkb/user.go‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"io"
1111
"regexp"
12+
"time"
1213

1314
keybase1 "github.com/keybase/client/go/protocol/keybase1"
1415
stellar1 "github.com/keybase/client/go/protocol/stellar1"
@@ -824,6 +825,75 @@ func (u *User) IsCachedIdentifyFresh(upk *keybase1.UserPlusKeysV2AllIncarnations
824825
return true
825826
}
826827

828+
func LoadHasRandomPw(mctx MetaContext, arg keybase1.LoadHasRandomPwArg) (res bool, err error) {
829+
mctx = mctx.WithLogTag("HASRPW")
830+
defer mctx.TraceTimed(fmt.Sprintf("User#LoadHasRandomPw(forceRepoll=%t)", arg.ForceRepoll), func() error { return err })()
831+
832+
cacheKey := DbKey{
833+
Typ: DBHasRandomPW,
834+
Key: mctx.ActiveDevice().UID().String(),
835+
}
836+
837+
var cachedValue, hasCache bool
838+
if !arg.ForceRepoll {
839+
if hasCache, err = mctx.G().GetKVStore().GetInto(&cachedValue, cacheKey); err == nil {
840+
if hasCache && !cachedValue {
841+
mctx.Debug("Returning HasRandomPW=false from KVStore cache")
842+
return false, nil
843+
}
844+
// If it was never cached or user *IS* RandomPW right now, pass through
845+
// and call the API.
846+
} else {
847+
mctx.Debug("Unable to get cached value for HasRandomPW: %v", err)
848+
}
849+
}
850+
851+
var initialTimeout time.Duration
852+
if !arg.ForceRepoll {
853+
// If we are do not need accurate response from the API server, make
854+
// the request with a timeout for quicker overall RPC response time
855+
// if network is bad/unavailable.
856+
initialTimeout = 3 * time.Second
857+
}
858+
859+
var ret struct {
860+
AppStatusEmbed
861+
RandomPW bool `json:"random_pw"`
862+
}
863+
err = mctx.G().API.GetDecode(mctx, APIArg{
864+
Endpoint: "user/has_random_pw",
865+
SessionType: APISessionTypeREQUIRED,
866+
InitialTimeout: initialTimeout,
867+
}, &ret)
868+
if err != nil {
869+
if !arg.ForceRepoll {
870+
if hasCache {
871+
// We are allowed to return cache if we have any.
872+
mctx.Warning("Unable to make a network request to has_random_pw. Returning cached value: %t", cachedValue)
873+
return cachedValue, nil
874+
}
875+
876+
mctx.Warning("Unable to make a network request to has_random_pw and there is no cache. Erroring out.")
877+
}
878+
return res, err
879+
}
880+
881+
if !hasCache || cachedValue != ret.RandomPW {
882+
// Cache current state. If we put `randomPW=false` in the cache, we will never
883+
// ever have to call to the network from this device, because it's not possible
884+
// to become `randomPW=true` again. If we cache `randomPW=true` we are going to
885+
// keep asking the network, but we will be resilient to bad network conditions
886+
// because we will have this cached state to fall back on.
887+
if err := mctx.G().GetKVStore().PutObj(cacheKey, nil, ret.RandomPW); err == nil {
888+
mctx.Debug("Adding HasRandomPW=%t to KVStore", ret.RandomPW)
889+
} else {
890+
mctx.Debug("Unable to add HasRandomPW state to KVStore")
891+
}
892+
}
893+
894+
return ret.RandomPW, err
895+
}
896+
827897
// PartialCopy copies some fields of the User object, but not all.
828898
// For instance, it doesn't copy the SigChain or IDTable, and it only
829899
// makes a shallow copy of the ComputedKeyFamily.

‎go/protocol/keybase1/pgp_ui.go‎

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎go/service/pgp.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ func (u *RemotePgpUI) KeyGenerated(ctx context.Context, arg keybase1.KeyGenerate
3636
return u.cli.KeyGenerated(ctx, arg)
3737
}
3838

39-
func (u *RemotePgpUI) ShouldPushPrivate(ctx context.Context, sessionID int) (bool, error) {
40-
return u.cli.ShouldPushPrivate(ctx, u.sessionID)
39+
func (u *RemotePgpUI) ShouldPushPrivate(ctx context.Context, arg keybase1.ShouldPushPrivateArg) (bool, error) {
40+
arg.SessionID = u.sessionID
41+
return u.cli.ShouldPushPrivate(ctx, arg)
4142
}
4243

4344
func (u *RemotePgpUI) Finished(ctx context.Context, sessionID int) error {

0 commit comments

Comments
 (0)