func validateRoot(gun string, oldRoot, newRoot []byte) (*data.SignedRoot, error) { var parsedOldRoot *data.SignedRoot parsedNewRoot := &data.SignedRoot{} if oldRoot != nil { parsedOldRoot = &data.SignedRoot{} err := json.Unmarshal(oldRoot, parsedOldRoot) if err != nil { // TODO(david): if we can't read the old root should we continue // here to check new root self referential integrity? // This would permit recovery of a repo with a corrupted // root. logrus.Warn("Old root could not be parsed.") } } err := json.Unmarshal(newRoot, parsedNewRoot) if err != nil { return nil, err } if err := checkRoot(parsedOldRoot, parsedNewRoot); err != nil { // TODO(david): how strict do we want to be here about old signatures // for rotations? Should the user have to provide a flag // which gets transmitted to force a root update without // correct old key signatures. return nil, err } if !data.ValidTUFType(parsedNewRoot.Signed.Type, data.CanonicalRootRole) { return nil, fmt.Errorf("root has wrong type") } return parsedNewRoot, nil }
func validateSnapshot(role string, oldSnap *data.SignedSnapshot, snapUpdate storage.MetaUpdate, roles map[string]storage.MetaUpdate, kdb *keys.KeyDB) error { s := &data.Signed{} err := json.Unmarshal(snapUpdate.Data, s) if err != nil { return errors.New("could not parse snapshot") } // version specifically gets validated when writing to store to // better handle race conditions there. if err := signed.Verify(s, role, 0, kdb); err != nil { return err } snap, err := data.SnapshotFromSigned(s) if err != nil { return errors.New("could not parse snapshot") } if !data.ValidTUFType(snap.Signed.Type, data.CanonicalSnapshotRole) { return errors.New("snapshot has wrong type") } err = checkSnapshotEntries(role, oldSnap, snap, roles) if err != nil { return err } return nil }
func Verify(s *data.Signed, role string, minVersion int, db *keys.KeyDB) error { if err := VerifySignatures(s, role, db); err != nil { return err } sm := &signedMeta{} if err := json.Unmarshal(s.Signed, sm); err != nil { return err } // This is not the valid way to check types as all targets files will // have the "Targets" type. //if strings.ToLower(sm.Type) != strings.ToLower(role) { // return ErrWrongType //} if !data.ValidTUFType(sm.Type) { return ErrWrongType } if IsExpired(sm.Expires) { logrus.Errorf("Metadata for %s expired", role) return ErrExpired{sm.Expires} } if sm.Version < minVersion { return ErrLowVersion{sm.Version, minVersion} } return nil }
func verifyMeta(s *data.Signed, role string, minVersion int) error { sm := &data.SignedCommon{} if err := json.Unmarshal(s.Signed, sm); err != nil { return err } if !data.ValidTUFType(sm.Type, role) { return ErrWrongType } if IsExpired(sm.Expires) { logrus.Errorf("Metadata for %s expired", role) return ErrExpired{Role: role, Expired: sm.Expires.Format("Mon Jan 2 15:04:05 MST 2006")} } if sm.Version < minVersion { return ErrLowVersion{sm.Version, minVersion} } return nil }
func verifyMeta(s *data.Signed, role string, minVersion int) error { sm := &signedMeta{} if err := json.Unmarshal(s.Signed, sm); err != nil { return err } if !data.ValidTUFType(sm.Type) { return ErrWrongType } if IsExpired(sm.Expires) { logrus.Errorf("Metadata for %s expired", role) return ErrExpired{sm.Expires} } if sm.Version < minVersion { return ErrLowVersion{sm.Version, minVersion} } return nil }
func validateTargets(role string, roles map[string]storage.MetaUpdate, kdb *keys.KeyDB) (*data.SignedTargets, error) { // TODO: when delegations are being validated, validate parent // role exists for any delegation s := &data.Signed{} err := json.Unmarshal(roles[role].Data, s) if err != nil { return nil, fmt.Errorf("could not parse %s", role) } // version specifically gets validated when writing to store to // better handle race conditions there. if err := signed.Verify(s, role, 0, kdb); err != nil { return nil, err } t, err := data.TargetsFromSigned(s) if err != nil { return nil, err } if !data.ValidTUFType(t.Signed.Type, data.CanonicalTargetsRole) { return nil, fmt.Errorf("%s has wrong type", role) } return t, nil }