@@ -93,6 +93,7 @@ func (l LinkID) Eq(i2 LinkID) bool {
9393type 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+
285291func (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+
493545func (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+
10211095func (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+
11841274func (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