forked from theupdateframework/go-tuf
-
Notifications
You must be signed in to change notification settings - Fork 0
Support root rotation #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f7da156
Add chain root download
raphaelgavache 097cd40
Merge branch 'master' of github.com:DataDog/go-tuf into hosseinsia/up…
hosseinsia 0b3bf85
imporved updateRoots implmenetation -- not tested yet
hosseinsia a708650
refactor client delegation test; extract a test helper, clienttest, t…
hosseinsia 8ed01d6
refactor client delegation test; extract a test helper, clienttest, t…
hosseinsia 07e5fd2
add test fixtures for root key rotation
hosseinsia a24ca55
add a test for success case
hosseinsia 23794b2
add a test for wrong root version.
hosseinsia 8b758e2
add a test for the invalid root signature.
hosseinsia 04e062b
add more testcases for root update
hosseinsia d06ba58
remove an unsed method
hosseinsia de210b9
remove some old comments
hosseinsia bc4a1e7
remove the timestamp and snapshot files if root is rotated and they h…
hosseinsia 5f9d948
fast forward attack detection, and clean up
hosseinsia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -6,6 +6,8 @@ import ( | |||||
"errors" | ||||||
"io" | ||||||
"io/ioutil" | ||||||
"reflect" | ||||||
"sort" | ||||||
|
||||||
"github.com/theupdateframework/go-tuf/data" | ||||||
"github.com/theupdateframework/go-tuf/util" | ||||||
|
@@ -14,11 +16,11 @@ import ( | |||||
|
||||||
const ( | ||||||
// This is the upper limit in bytes we will use to limit the download | ||||||
// size of the root/timestamp roles, since we might not don't know how | ||||||
// big it is. | ||||||
// size of the root/timestamp roles, since the size is unknown. | ||||||
defaultRootDownloadLimit = 512000 | ||||||
defaultTimestampDownloadLimit = 16384 | ||||||
defaultMaxDelegations = 32 | ||||||
defaultMaxRoots = 10000 | ||||||
) | ||||||
|
||||||
// LocalStore is local storage for downloaded top-level metadata. | ||||||
|
@@ -86,13 +88,19 @@ type Client struct { | |||||
// MaxDelegations limits by default the number of delegations visited for any | ||||||
// target | ||||||
MaxDelegations int | ||||||
|
||||||
// ChainedRootUpdater enables https://theupdateframework.github.io/specification/v1.0.19/index.html#update-root | ||||||
ChainedRootUpdater bool | ||||||
// UpdaterMaxRoots limits the number of downloaded roots in 1.0.19 root updater | ||||||
UpdaterMaxRoots int | ||||||
} | ||||||
|
||||||
func NewClient(local LocalStore, remote RemoteStore) *Client { | ||||||
return &Client{ | ||||||
local: local, | ||||||
remote: remote, | ||||||
MaxDelegations: defaultMaxDelegations, | ||||||
local: local, | ||||||
remote: remote, | ||||||
MaxDelegations: defaultMaxDelegations, | ||||||
UpdaterMaxRoots: defaultMaxRoots, | ||||||
} | ||||||
} | ||||||
|
||||||
|
@@ -144,6 +152,13 @@ func (c *Client) Init(rootKeys []*data.Key, threshold int) error { | |||||
// | ||||||
// https://github.com/theupdateframework/tuf/blob/v0.9.9/docs/tuf-spec.txt#L714 | ||||||
func (c *Client) Update() (data.TargetFiles, error) { | ||||||
if c.ChainedRootUpdater { | ||||||
if err := c.updateRoots(); err != nil { | ||||||
return data.TargetFiles{}, err | ||||||
} | ||||||
return c.update(true) | ||||||
} | ||||||
|
||||||
return c.update(false) | ||||||
} | ||||||
|
||||||
|
@@ -249,6 +264,138 @@ func (c *Client) update(latestRoot bool) (data.TargetFiles, error) { | |||||
return updatedTargets, nil | ||||||
} | ||||||
|
||||||
func (c *Client) updateRoots() error { | ||||||
// https://theupdateframework.github.io/specification/v1.0.19/index.html#load-trusted-root | ||||||
|
||||||
// 5.2 Load the trusted root metadata file. We assume that a good, | ||||||
// trusted copy of this file was shipped with the package manager | ||||||
// or software updater using an out-of-band process. Note that | ||||||
// the expiration of the trusted root metadata file does not | ||||||
// matter, because we will attempt to update it in the next step. | ||||||
if err := c.getLocalMeta(); err != nil { | ||||||
if _, ok := err.(verify.ErrExpired); !ok { | ||||||
return err | ||||||
} | ||||||
} | ||||||
|
||||||
// Prepare for 5.3.11: If the timestamp and / or snapshot keys have been rotated, | ||||||
// then delete the trusted timestamp and snapshot metadata files. | ||||||
getKeyIDs := func(role string) []string { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
keyIDs := make([]string, 0, len(c.db.GetRole("timestamp").KeyIDs)) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
for k := range c.db.GetRole(role).KeyIDs { | ||||||
keyIDs = append(keyIDs, k) | ||||||
} | ||||||
sort.Strings(keyIDs) | ||||||
return keyIDs | ||||||
} | ||||||
startTimestampKeyIDs := getKeyIDs("timestamp") | ||||||
startSnapshotKeyIDs := getKeyIDs("snapshot") | ||||||
|
||||||
// 5.3.1 Temorarily turn on the consistent snapshots in order to download | ||||||
// versioned root metadata files as described next. | ||||||
consistentSnapshot := c.consistentSnapshot | ||||||
c.consistentSnapshot = true | ||||||
|
||||||
// https://theupdateframework.github.io/specification/v1.0.19/index.html#update-root | ||||||
|
||||||
// 5.3.1 Since it may now be signed using entirely different keys, | ||||||
// the client MUST somehow be able to establish a trusted line of | ||||||
// continuity to the latest set of keys (see § 6.1 Key | ||||||
// management and migration). To do so, the client MUST | ||||||
// download intermediate root metadata files, until the | ||||||
// latest available one is reached. Therefore, it MUST | ||||||
// temporarily turn on consistent snapshots in order to | ||||||
// download versioned root metadata files as described next. | ||||||
|
||||||
// This loop returns on error or breaks after downloading the lastest root metadata. | ||||||
// 5.3.2 Let N denote the version number of the trusted root metadata file. | ||||||
for i := 0; i < c.UpdaterMaxRoots; i++ { | ||||||
// 5.3.3 Try downloading version nPlusOne of the root metadata file. | ||||||
// NOTE: as a side effect, we do update c.rootVer to nPlusOne between iterations. | ||||||
nPlusOne := c.rootVer + 1 | ||||||
nPlusOneRootPath := util.VersionedPath("root.json", nPlusOne) | ||||||
nPlusOneRootMetadata, err := c.downloadMetaUnsafe(nPlusOneRootPath, defaultRootDownloadLimit) | ||||||
//fmt.Println(string(nPlusOneRootMetadata[:])) | ||||||
if err != nil { | ||||||
if _, ok := err.(ErrMissingRemoteMetadata); ok { | ||||||
// stop when the next root can't be downloaded | ||||||
break | ||||||
} else { | ||||||
return err | ||||||
} | ||||||
} | ||||||
|
||||||
// 5.3.4 Check for an arbitrary software attack. | ||||||
nPlusOneRootMetadataSigned := &data.Root{} | ||||||
// 5.3.4.1 Check that N signed N+1 | ||||||
// 5.3.5 Check for a rollback attack. Here, we check that nPlusOneRootMetadataSigned.version >= nPlusOne. | ||||||
if err := c.db.UnmarshalExpired(nPlusOneRootMetadata, nPlusOneRootMetadataSigned, "root", nPlusOne); err != nil { | ||||||
// 5.3.6 Note that the expiration of the new (intermediate) root | ||||||
// metadata file does not matter yet, because we will check for | ||||||
// it in step 5.3.10. | ||||||
if _, ok := err.(verify.ErrExpired); !ok { | ||||||
return err | ||||||
} | ||||||
} | ||||||
|
||||||
// 5.3.5 Following up, we check for a fast-forward attack: here, we check for that nPlusOneRootMetadataSigned.version >= nPlusOne. | ||||||
if nPlusOneRootMetadataSigned.Version != nPlusOne { | ||||||
return verify.ErrWrongVersion{ | ||||||
Given: nPlusOneRootMetadataSigned.Version, | ||||||
Expected: nPlusOne, | ||||||
} | ||||||
} | ||||||
|
||||||
// 5.3.7 Set the trusted root metadata file to the new root metadata file. | ||||||
c.rootVer = nPlusOneRootMetadataSigned.Version | ||||||
// NOTE: following up on 5.3.1, we want to always have consistent snapshots on for the duration | ||||||
// of root rotation. AFTER the rotation is over, we will set it to the value of the last root. | ||||||
consistentSnapshot = nPlusOneRootMetadataSigned.ConsistentSnapshot | ||||||
// 5.3.8 Persist root metadata. The client MUST write the file to non-volatile storage as FILENAME.EXT (e.g. root.json). | ||||||
// NOTE: Internally, setMeta stores metadata in LevelDB in a persistent manner. | ||||||
if err := c.local.SetMeta("root.json", nPlusOneRootMetadata); err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
// 5.3.4.2 check that N+1 signed itself. | ||||||
if err := c.getLocalMeta(); err != nil { | ||||||
// 5.3.6 Note that the expiration of the new (intermediate) root | ||||||
// metadata file does not matter yet, because we will check for | ||||||
// it in step 5.3.10. | ||||||
if _, ok := err.(verify.ErrExpired); !ok { | ||||||
return err | ||||||
} | ||||||
} | ||||||
|
||||||
// 5.3.9 Repeat steps 5.3.2 to 5.3.9 | ||||||
} | ||||||
|
||||||
// 5.3.10 Check for a freeze attack. | ||||||
// NOTE: this will check for any, including freeze, attack. | ||||||
if err := c.getLocalMeta(); err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
// 5.3.11 If the timestamp and / or snapshot keys have been rotated, | ||||||
// then delete the trusted timestamp and snapshot metadata files. | ||||||
// To figure out if the keys are rotated, compare the timestamp/snapshot keys | ||||||
// of start root and end root. | ||||||
//endTimestampKeyIDs := someFunc(c.db.GetRole("timestamp").KeyIDs) | ||||||
endTimestampKeyIDs := getKeyIDs("timestamp") | ||||||
endSnapshotKeyIDs := getKeyIDs("snapshot") | ||||||
|
||||||
if !reflect.DeepEqual(startTimestampKeyIDs, endTimestampKeyIDs) || | ||||||
!reflect.DeepEqual(startSnapshotKeyIDs, endSnapshotKeyIDs) { | ||||||
c.local.SetMeta("snapshot", json.RawMessage{}) | ||||||
c.local.SetMeta("timestamp", json.RawMessage{}) | ||||||
return ErrEmptyTimestampOrSnapshot | ||||||
} | ||||||
|
||||||
// 5.3.12 Set whether consistent snapshots are used as per the trusted root metadata file. | ||||||
c.consistentSnapshot = consistentSnapshot | ||||||
return nil | ||||||
} | ||||||
|
||||||
func (c *Client) updateWithLatestRoot(m *data.SnapshotFileMeta) (data.TargetFiles, error) { | ||||||
var rootJSON json.RawMessage | ||||||
var err error | ||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about a more descriptive name like
MaxRootRotations
?