|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/theupdateframework/go-tuf/data" |
| 5 | + "github.com/theupdateframework/go-tuf/verify" |
| 6 | +) |
| 7 | + |
| 8 | +// getTargetFileMeta searches for a verified TargetFileMeta matching a file name |
| 9 | +// Requires a local snapshot to be loaded and is locked to the snapshot versions. |
| 10 | +// Searches through delegated targets following TUF spec 1.0.19 section 5.6. |
| 11 | +func (c *Client) getTargetFileMeta(file string) (data.TargetFileMeta, error) { |
| 12 | + snapshot, err := c.loadLocalSnapshot() |
| 13 | + if err != nil { |
| 14 | + return data.TargetFileMeta{}, err |
| 15 | + } |
| 16 | + verifiers := map[string]verify.DelegationsVerifier{"root": c.db} |
| 17 | + |
| 18 | + // delegationsIterator covers 5.6.7 |
| 19 | + // - pre-order depth-first search starting with the top targets |
| 20 | + // - filter delegations with paths or path_hash_prefixes matching searched file |
| 21 | + // - 5.6.7.1 cycles protection |
| 22 | + // - 5.6.7.2 terminations |
| 23 | + delegations := newDelegationsIterator(c.rootTargetDelegation(), "root", file) |
| 24 | + for i := 0; i < c.MaxDelegations; i++ { |
| 25 | + d, ok := delegations.next() |
| 26 | + if !ok { |
| 27 | + return data.TargetFileMeta{}, ErrUnknownTarget{file, snapshot.Version} |
| 28 | + } |
| 29 | + verifier := verifiers[d.parent] |
| 30 | + // covers 5.6.{1,2,3,4,5,6} |
| 31 | + target, err := c.loadDelegatedTargets(snapshot, d.child.Name, verifier) |
| 32 | + if err != nil { |
| 33 | + return data.TargetFileMeta{}, err |
| 34 | + } |
| 35 | + // stop when the searched TargetFileMeta is found |
| 36 | + if m, ok := target.Targets[file]; ok { |
| 37 | + return m, nil |
| 38 | + } |
| 39 | + if target.Delegations != nil { |
| 40 | + delegations.add(target.Delegations.Roles, d.child.Name) |
| 41 | + targetVerifier, err := verify.NewDelegationsVerifier(target.Delegations) |
| 42 | + if err != nil { |
| 43 | + return data.TargetFileMeta{}, err |
| 44 | + } |
| 45 | + verifiers[d.child.Name] = targetVerifier |
| 46 | + } |
| 47 | + } |
| 48 | + return data.TargetFileMeta{}, ErrMaxDelegations{ |
| 49 | + File: file, |
| 50 | + MaxDelegations: c.MaxDelegations, |
| 51 | + SnapshotVersion: snapshot.Version, |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func (c *Client) loadLocalSnapshot() (*data.Snapshot, error) { |
| 56 | + if err := c.getLocalMeta(); err != nil { |
| 57 | + return nil, err |
| 58 | + } |
| 59 | + rawS, ok := c.localMeta["snapshot.json"] |
| 60 | + if !ok { |
| 61 | + return nil, ErrNoLocalSnapshot |
| 62 | + } |
| 63 | + snapshot := &data.Snapshot{} |
| 64 | + if err := c.db.Unmarshal(rawS, snapshot, "snapshot", c.snapshotVer); err != nil { |
| 65 | + return nil, ErrDecodeFailed{"snapshot.json", err} |
| 66 | + } |
| 67 | + return snapshot, nil |
| 68 | +} |
| 69 | + |
| 70 | +// loadDelegatedTargets downloads, decodes, verifies and stores delegated targets |
| 71 | +func (c *Client) loadDelegatedTargets(snapshot *data.Snapshot, role string, verifier verify.DelegationsVerifier) (*data.Targets, error) { |
| 72 | + var err error |
| 73 | + fileName := role + ".json" |
| 74 | + fileMeta, ok := snapshot.Meta[fileName] |
| 75 | + if !ok { |
| 76 | + return nil, ErrRoleNotInSnapshot{role, snapshot.Version} |
| 77 | + } |
| 78 | + // 5.6.1 download target if not in the local store |
| 79 | + // 5.6.2 check against snapshot hash |
| 80 | + raw, alreadyStored := c.localMetaFromSnapshot(fileName, fileMeta) |
| 81 | + if !alreadyStored { |
| 82 | + raw, err = c.downloadMetaFromSnapshot(fileName, fileMeta) |
| 83 | + if err != nil { |
| 84 | + return nil, err |
| 85 | + } |
| 86 | + } |
| 87 | + target := &data.Targets{} |
| 88 | + // 5.6.3 verify signature with parent public keys |
| 89 | + // 5.6.5 verify that the targets is not expired |
| 90 | + if err := verifier.Unmarshal(raw, target, role, fileMeta.Version); err != nil { |
| 91 | + return nil, ErrDecodeFailed{fileName, err} |
| 92 | + } |
| 93 | + // 5.6.4 check against snapshot version |
| 94 | + if target.Version != fileMeta.Version { |
| 95 | + return nil, ErrTargetsSnapshotVersionMismatch{ |
| 96 | + Role: fileName, |
| 97 | + DownloadedTargetsVersion: fileMeta.Version, |
| 98 | + TargetsSnapshotVersion: target.Version, |
| 99 | + SnapshotVersion: snapshot.Version, |
| 100 | + } |
| 101 | + } |
| 102 | + // 5.6.6 persist |
| 103 | + if !alreadyStored { |
| 104 | + if err := c.local.SetMeta(fileName, raw); err != nil { |
| 105 | + return nil, err |
| 106 | + } |
| 107 | + } |
| 108 | + return target, nil |
| 109 | +} |
| 110 | + |
| 111 | +func (c *Client) rootTargetDelegation() data.DelegatedRole { |
| 112 | + role := "targets" |
| 113 | + r := c.db.GetRole(role) |
| 114 | + if r == nil { |
| 115 | + return data.DelegatedRole{} |
| 116 | + } |
| 117 | + keyIDs := make([]string, 0, len(r.KeyIDs)) |
| 118 | + for id, _ := range r.KeyIDs { |
| 119 | + keyIDs = append(keyIDs, id) |
| 120 | + } |
| 121 | + return data.DelegatedRole{ |
| 122 | + Name: role, |
| 123 | + KeyIDs: keyIDs, |
| 124 | + Threshold: r.Threshold, |
| 125 | + Paths: []string{"*"}, |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +type delegation struct { |
| 130 | + parent string |
| 131 | + child data.DelegatedRole |
| 132 | +} |
| 133 | + |
| 134 | +type delegationID struct { |
| 135 | + parent string |
| 136 | + child string |
| 137 | +} |
| 138 | + |
| 139 | +type delegationsIterator struct { |
| 140 | + stack []delegation |
| 141 | + file string |
| 142 | + visited map[delegationID]struct{} |
| 143 | +} |
| 144 | + |
| 145 | +func newDelegationsIterator(role data.DelegatedRole, parent string, file string) *delegationsIterator { |
| 146 | + i := &delegationsIterator{ |
| 147 | + file: file, |
| 148 | + stack: make([]delegation, 0, 1), |
| 149 | + visited: make(map[delegationID]struct{}), |
| 150 | + } |
| 151 | + i.add([]data.DelegatedRole{role}, parent) |
| 152 | + return i |
| 153 | +} |
| 154 | + |
| 155 | +func (d *delegationsIterator) next() (delegation, bool) { |
| 156 | + if len(d.stack) == 0 { |
| 157 | + return delegation{}, false |
| 158 | + } |
| 159 | + delegation := d.stack[len(d.stack)-1] |
| 160 | + d.stack = d.stack[:len(d.stack)-1] |
| 161 | + |
| 162 | + // 5.6.7.1 cycles protection |
| 163 | + id := delegationID{delegation.parent, delegation.child.Name} |
| 164 | + if _, ok := d.visited[id]; ok { |
| 165 | + return d.next() |
| 166 | + } |
| 167 | + d.visited[id] = struct{}{} |
| 168 | + |
| 169 | + // 5.6.7.2 trim delegations to visit, only the current role and its delegations |
| 170 | + // will be considered |
| 171 | + // https://github.com/theupdateframework/specification/issues/168 |
| 172 | + if delegation.child.Terminating { |
| 173 | + d.stack = d.stack[0:0] |
| 174 | + } |
| 175 | + return delegation, true |
| 176 | +} |
| 177 | + |
| 178 | +func (d *delegationsIterator) add(roles []data.DelegatedRole, parent string) { |
| 179 | + for i := len(roles) - 1; i >= 0; i-- { |
| 180 | + r := roles[i] |
| 181 | + if r.MatchesPath(d.file) { |
| 182 | + d.stack = append(d.stack, delegation{parent, r}) |
| 183 | + } |
| 184 | + } |
| 185 | +} |
0 commit comments