Skip to content

Commit cdbe03f

Browse files
xgessheronhaye
authored andcommitted
* Reading and writing high skips on userchains (feature-flagged)
* Reading and writing high skips on userchains (feature-flagged)
1 parent 3782ef5 commit cdbe03f

23 files changed

Lines changed: 686 additions & 78 deletions

‎go/engine/cryptocurrency.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ func (e *CryptocurrencyEngine) Run(m libkb.MetaContext) (err error) {
109109
}
110110

111111
sig, _, _, err := libkb.MakeSig(
112+
m,
112113
sigKey,
113114
libkb.LinkTypeCryptocurrency,
114115
sigInner,

‎go/engine/prove.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ func (p *Prove) generateProof(m libkb.MetaContext) (err error) {
196196
}
197197

198198
p.sig, p.sigID, _, err = libkb.MakeSig(
199+
m,
199200
p.signingKey,
200201
libkb.LinkTypeWebServiceBinding,
201202
p.sigInner,

‎go/engine/revoke_test.go‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,24 @@ func testRevokePaperDevice(t *testing.T, upgradePerUserKey bool) {
141141
} else {
142142
checkPerUserKeyring(t, tc.G, 0)
143143
}
144+
145+
arg := libkb.NewLoadUserByNameArg(tc.G, u.Username)
146+
user, err := libkb.LoadUser(arg)
147+
require.NoError(t, err)
148+
149+
var nextSeqno int
150+
var postedSeqno int
151+
if upgradePerUserKey {
152+
nextSeqno = 7
153+
postedSeqno = 4
154+
} else {
155+
nextSeqno = 5
156+
postedSeqno = 3
157+
}
158+
nextExpected, err := user.GetExpectedNextHighSkip(libkb.NewMetaContextForTest(tc))
159+
require.NoError(t, err)
160+
require.Equal(t, nextExpected.Seqno, keybase1.Seqno(nextSeqno))
161+
assertPostedHighSkipSeqno(t, tc, user.GetName(), postedSeqno)
144162
}
145163

146164
func TestRevokerPaperDeviceTwice(t *testing.T) {

‎go/engine/track_token.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ func (e *TrackToken) storeRemoteTrack(m libkb.MetaContext, pubKID keybase1.KID)
226226

227227
sigVersion := libkb.SigVersion(*e.arg.Options.SigVersion)
228228
sig, sigID, linkID, err := libkb.MakeSig(
229+
m,
229230
signingKey,
230231
libkb.LinkTypeTrack,
231232
e.trackStatementBytes,
@@ -264,7 +265,7 @@ func (e *TrackToken) storeRemoteTrack(m libkb.MetaContext, pubKID keybase1.KID)
264265
return err
265266
}
266267

267-
me.SigChainBump(linkID, sigID)
268+
me.SigChainBump(linkID, sigID, false)
268269

269270
return err
270271
}

‎go/engine/untrack.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ func (e *UntrackEngine) storeRemoteUntrack(m libkb.MetaContext, them *libkb.User
205205

206206
sigVersion := libkb.SigVersion(e.arg.SigVersion)
207207
sig, sigID, _, err := libkb.MakeSig(
208+
m,
208209
signingKey,
209210
libkb.LinkTypeUntrack,
210211
e.untrackStatementBytes,

‎go/engine/user_test.go‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,46 @@ func TestMerkleHashMetaAndFirstAppearedInKeyFamily(t *testing.T) {
115115
checkSubkey(subkey.GetKID())
116116
}
117117
}
118+
119+
func assertPostedHighSkipSeqno(t *testing.T, tc libkb.TestContext, name string, seqno int) {
120+
u, err := libkb.LoadUser(libkb.NewLoadUserByNameArg(tc.G, name))
121+
if err != nil {
122+
t.Fatal(err)
123+
}
124+
125+
highSkip := u.GetLastLink().GetHighSkip()
126+
require.Equal(t, highSkip.Seqno, keybase1.Seqno(seqno))
127+
}
128+
129+
func TestBlankUserHighSkip(t *testing.T) {
130+
tc := SetupEngineTest(t, "user")
131+
defer tc.Cleanup()
132+
133+
i := CreateAndSignupFakeUser(tc, "login")
134+
135+
assertPostedHighSkipSeqno(t, tc, i.Username, 1)
136+
}
137+
138+
func TestPaperUserHighSkip(t *testing.T) {
139+
tc := SetupEngineTest(t, "user")
140+
defer tc.Cleanup()
141+
them, _ := createFakeUserWithNoKeys(tc)
142+
143+
i := CreateAndSignupFakeUserPaper(tc, "login")
144+
assertPostedHighSkipSeqno(t, tc, i.Username, 4)
145+
146+
trackUser(tc, i, libkb.NewNormalizedUsername(them), libkb.GetDefaultSigVersion(tc.G))
147+
assertPostedHighSkipSeqno(t, tc, i.Username, 4)
148+
149+
eng := NewPaperKey(tc.G)
150+
uis := libkb.UIs{
151+
LogUI: tc.G.UI.GetLogUI(),
152+
LoginUI: &libkb.TestLoginUI{},
153+
SecretUI: &libkb.TestSecretUI{},
154+
}
155+
m := NewMetaContextForTest(tc).WithUIs(uis)
156+
if err := RunEngine2(m, eng); err != nil {
157+
t.Fatal(err)
158+
}
159+
assertPostedHighSkipSeqno(t, tc, i.Username, 7)
160+
}

‎go/libkb/chain_link.go‎

Lines changed: 142 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ func (l LinkID) Eq(i2 LinkID) bool {
9393
type ChainLinkUnpacked struct {
9494
prev LinkID
9595
seqno keybase1.Seqno
96+
highSkip *HighSkip
9697
seqType keybase1.SeqType
9798
ignoreIfUnsupported SigIgnoreIfUnsupported
9899
payloadLocal []byte // local track payloads
@@ -226,6 +227,7 @@ type ChainLink struct {
226227
unsigned bool
227228
dirty bool
228229
revocationsCache *[]keybase1.SigID
230+
computedHighSkip *HighSkip
229231

230232
unpacked *ChainLinkUnpacked
231233
cki *ComputedKeyInfos
@@ -282,6 +284,10 @@ func (c *ChainLink) GetIgnoreIfSupported() SigIgnoreIfUnsupported {
282284
return c.getIgnoreIfUnsupportedFromPayload()
283285
}
284286

287+
func (c *ChainLink) getHighSkipFromPayload() *HighSkip {
288+
return c.unpacked.highSkip
289+
}
290+
285291
func (c *ChainLink) IsStubbed() bool {
286292
return c.unpacked.stubbed
287293
}
@@ -490,6 +496,52 @@ func (c *ChainLink) checkAgainstMerkleTree(t *MerkleTriple) (found bool, err err
490496
return
491497
}
492498

499+
func (tmp *ChainLinkUnpacked) parseHighSkipFromPayload(payload []byte) (*HighSkip, error) {
500+
hs, dataType, _, err := jsonparser.Get(payload, "high_skip")
501+
// high_skip is optional, but must be an object if it exists
502+
if err != nil {
503+
if err == jsonparser.KeyPathNotFoundError {
504+
return nil, nil
505+
}
506+
return nil, err
507+
}
508+
509+
if dataType != jsonparser.Object {
510+
return nil, ChainLinkError{fmt.Sprintf("When provided, expected high_skip to be a JSON object, was %v.", dataType)}
511+
}
512+
513+
highSkipSeqnoInt, err := jsonparser.GetInt(hs, "seqno")
514+
if err != nil {
515+
return nil, err
516+
}
517+
518+
// highSkipHash can either be null (zero-value of a LinkID) or a hexstring.
519+
// We call GetString first instead of Get so we only parse the value
520+
// twice for the first link.
521+
highSkipHashStr, err := jsonparser.GetString(hs, "hash")
522+
var highSkipHash LinkID
523+
if err != nil {
524+
// If there was an error parsing as a string, make sure the value is null.
525+
_, dataType, _, getErr := jsonparser.Get(hs, "hash")
526+
if getErr != nil {
527+
return nil, getErr
528+
}
529+
if dataType != jsonparser.Null {
530+
return nil, ChainLinkError{
531+
fmt.Sprintf("high_skip.hash was neither a valid string (%v) nor null.", err.Error()),
532+
}
533+
}
534+
} else {
535+
highSkipHash, err = LinkIDFromHex(highSkipHashStr)
536+
if err != nil {
537+
return nil, err
538+
}
539+
}
540+
541+
highSkip := NewHighSkip(keybase1.Seqno(highSkipSeqnoInt), highSkipHash)
542+
return &highSkip, nil
543+
}
544+
493545
func (tmp *ChainLinkUnpacked) unpackPayloadJSON(g *GlobalContext, payload []byte) error {
494546
if s, err := jsonparser.GetString(payload, "body", "key", "fingerprint"); err == nil {
495547
if tmp.pgpFingerprint, err = PGPFingerprintFromHex(s); err != nil {
@@ -524,6 +576,12 @@ func (tmp *ChainLinkUnpacked) unpackPayloadJSON(g *GlobalContext, payload []byte
524576
}
525577
}
526578

579+
highSkip, err := tmp.parseHighSkipFromPayload(payload)
580+
if err != nil {
581+
return err
582+
}
583+
tmp.highSkip = highSkip
584+
527585
tmp.typ, err = jsonparser.GetString(payload, "body", "type")
528586
if err != nil {
529587
return err
@@ -638,11 +696,22 @@ func (c *ChainLink) unpackStubbed(raw string) error {
638696
}
639697

640698
c.id = ol.LinkID()
699+
700+
// Because the outer link does not have a highSkip parent object, we check
701+
// for the nullity of highSkipSeqno to see if highSkip should be set, since
702+
// a null highSkipHash is valid when specifying highSkip=0.
703+
var highSkipPtr *HighSkip
704+
if ol.HighSkipSeqno != nil {
705+
highSkip := NewHighSkip(*ol.HighSkipSeqno, *ol.HighSkipHash)
706+
highSkipPtr = &highSkip
707+
}
708+
641709
c.unpacked = &ChainLinkUnpacked{
642710
prev: ol.Prev,
643711
seqno: ol.Seqno,
644712
seqType: ol.SeqType,
645713
ignoreIfUnsupported: ol.IgnoreIfUnsupported,
714+
highSkip: highSkipPtr,
646715
sigVersion: ol.Version,
647716
outerLinkV2: ol,
648717
stubbed: true,
@@ -973,13 +1042,14 @@ func (c *ChainLink) verifyPayloadV2() error {
9731042
prev := c.getPrevFromPayload()
9741043
curr := c.getPayloadHash()
9751044
ignoreIfUnsupported := c.getIgnoreIfUnsupportedFromPayload()
976-
linkType, err := c.GetSigchainV2Type(SigIgnoreIfUnsupported(ignoreIfUnsupported))
1045+
linkType, err := c.GetSigchainV2TypeFromInner(SigIgnoreIfUnsupported(ignoreIfUnsupported))
9771046
if err != nil {
9781047
return err
9791048
}
9801049
seqType := c.getSeqTypeFromPayload()
1050+
highSkip := c.getHighSkipFromPayload()
9811051

982-
if err := ol.AssertFields(version, seqno, prev, curr, linkType, seqType, ignoreIfUnsupported); err != nil {
1052+
if err := ol.AssertFields(version, seqno, prev, curr, linkType, seqType, ignoreIfUnsupported, highSkip); err != nil {
9831053
return err
9841054
}
9851055

@@ -1018,6 +1088,10 @@ func (c *ChainLink) GetSeqno() keybase1.Seqno {
10181088
return c.unpacked.seqno
10191089
}
10201090

1091+
func (c *ChainLink) GetHighSkip() *HighSkip {
1092+
return c.unpacked.highSkip
1093+
}
1094+
10211095
func (c *ChainLink) GetSigID() keybase1.SigID {
10221096
return c.unpacked.sigID
10231097
}
@@ -1164,7 +1238,7 @@ func (c *ChainLink) verifyLinkV2() error {
11641238
return c.verifyPayloadV2()
11651239
}
11661240

1167-
func (c *ChainLink) GetSigchainV2Type(ignoreIfUnsupported SigIgnoreIfUnsupported) (SigchainV2Type, error) {
1241+
func (c *ChainLink) GetSigchainV2TypeFromInner(ignoreIfUnsupported SigIgnoreIfUnsupported) (SigchainV2Type, error) {
11681242
if c.unpacked == nil || c.unpacked.typ == "" {
11691243
return SigchainV2TypeNone, errors.New("chain link not unpacked")
11701244
}
@@ -1181,6 +1255,22 @@ func (c *ChainLink) GetSigchainV2TypeFromV2Shell() (SigchainV2Type, error) {
11811255
return c.unpacked.outerLinkV2.LinkType, nil
11821256
}
11831257

1258+
// GetSigchainV2Type is a helper function for getting a ChainLink's type. If it
1259+
// is a v2 link (that may or may not be stubbed), return the type from the
1260+
// outer link, otherwise from the inner link.
1261+
func (c *ChainLink) GetSigchainV2Type() (SigchainV2Type, error) {
1262+
if c.unpacked == nil {
1263+
return SigchainV2TypeNone, errors.New("chain link is not unpacked")
1264+
}
1265+
if c.unpacked.outerLinkV2 == nil && c.unpacked.typ == "" {
1266+
return SigchainV2TypeNone, errors.New("chain inner link type is not unpacked, and has no v2 shell")
1267+
}
1268+
if c.unpacked.outerLinkV2 != nil {
1269+
return c.GetSigchainV2TypeFromV2Shell()
1270+
}
1271+
return c.GetSigchainV2TypeFromInner(c.GetIgnoreIfSupported())
1272+
}
1273+
11841274
func (c *ChainLink) checkServerSignatureMetadata(ckf ComputedKeyFamily) (ret keybase1.KID, err error) {
11851275
var serverKID, linkKID, verifyKID keybase1.KID
11861276

@@ -1351,3 +1441,52 @@ func (c ChainLink) AllowStubbing() bool {
13511441
}
13521442
return c.unpacked.outerLinkV2.LinkType.AllowStubbing()
13531443
}
1444+
1445+
// IsHighUserLink determines whether a chainlink counts as "high" in a user's chain,
1446+
// which is defined as an Eldest link, a link with seqno=1, a link that is Sibkey,
1447+
// PGPUpdate, Revoke, or any link that is revoking.
1448+
func (c ChainLink) IsHighUserLink(mctx MetaContext, uid keybase1.UID) (bool, error) {
1449+
v2Type, err := c.GetSigchainV2Type()
1450+
if err != nil {
1451+
return false, err
1452+
}
1453+
1454+
hardcodedEldest := false
1455+
if c.GetSeqno() > 1 {
1456+
prevLink := c.parent.GetLinkFromSeqno(c.GetSeqno() - 1)
1457+
if prevLink == nil {
1458+
return false, ChainLinkWrongSeqnoError{}
1459+
}
1460+
hardcodedEldest, err = isSubchainStart(mctx, &c, prevLink, uid)
1461+
if err != nil {
1462+
return false, err
1463+
}
1464+
}
1465+
1466+
isFirstLink := v2Type == SigchainV2TypeEldest || c.GetSeqno() == 1 || hardcodedEldest
1467+
isNewHighLink := isFirstLink ||
1468+
v2Type == SigchainV2TypeRevoke ||
1469+
v2Type == SigchainV2TypeWebServiceBindingWithRevoke ||
1470+
v2Type == SigchainV2TypeCryptocurrencyWithRevoke ||
1471+
v2Type == SigchainV2TypeSibkey ||
1472+
v2Type == SigchainV2TypePGPUpdate
1473+
return isNewHighLink, nil
1474+
}
1475+
1476+
// ExpectedNextHighSkip returns the expected highSkip of the immediately
1477+
// subsequent link in the chain (which may not exist yet). This function can
1478+
// only be called after VerifyChain has processed the chainLink, and set
1479+
// c.computedHighSkip.
1480+
func (c ChainLink) ExpectedNextHighSkip(mctx MetaContext, uid keybase1.UID) (HighSkip, error) {
1481+
isHigh, err := c.IsHighUserLink(mctx, uid)
1482+
if err != nil {
1483+
return HighSkip{}, err
1484+
}
1485+
if isHigh {
1486+
return NewHighSkip(c.GetSeqno(), c.id), nil
1487+
}
1488+
if c.computedHighSkip == nil {
1489+
return HighSkip{}, NewUserReverifyNeededError("Expected to have already computed this link's HighSkip, but it was not computed.")
1490+
}
1491+
return *c.computedHighSkip, nil
1492+
}

0 commit comments

Comments
 (0)