/// Verify and update the targets metadata.
Verify and update the targets metadata.
Verify and update the targets metadata .
pub fn update_targets(
        &mut self,
        raw_targets: &RawSignedMetadata<D, TargetsMetadata>,
    ) -> Result<bool> {
        let verified = {
            // FIXME(https://github.com/theupdateframework/specification/issues/113) Checking if
            // this metadata expired isn't part of the spec. Do we actually want to do this?
            let trusted_root = self.trusted_root_unexpired()?;
            let trusted_snapshot = self.trusted_snapshot_unexpired()?;
            // FIXME(#295): TUF-1.0.5 §5.3.3.2 says this check should be done when updating the
            // snapshot, not here.
            let trusted_targets_description = trusted_snapshot
                .meta()
                .get(&MetadataPath::from_role(&Role::Targets))
                .ok_or_else(|| {
                    Error::VerificationFailure(
                        "Snapshot metadata had no description of the targets metadata".into(),
                    )
                })?;
            let trusted_targets_version = self.trusted_targets_version();
            match trusted_targets_description
                .version()
                .cmp(&trusted_targets_version)
            {
                Ordering::Less => {
                    return Err(Error::VerificationFailure(format!(
                        "Attempted to roll back targets metadata at version {} to {}.",
                        trusted_targets_version,
                        trusted_targets_description.version()
                    )));
                }
                Ordering::Equal => {
                    return Ok(false);
                }
                Ordering::Greater => {}
            }
            /////////////////////////////////////////
            // TUF-1.0.5 §5.4.1:
            //
            //     Check against snapshot metadata. The hashes and version number of the new
            //     targets metadata file MUST match the hashes (if any) and version number listed
            //     in the trusted snapshot metadata. This is done, in part, to prevent a
            //     mix-and-match attack by man-in-the-middle attackers. If the new targets metadata
            //     file does not match, discard it, abort the update cycle, and report the failure.
            // FIXME: rust-tuf checks the hash during download, but it would be better if we
            // checked the hash here to make it easier to validate we've correctly implemented the
            // spec.
            // NOTE(https://github.com/theupdateframework/specification/pull/112): Technically
            // we're supposed to check the version before checking the signature, but we do it
            // afterwards. That PR proposes formally moving the version check to after signature
            // verification.
            /////////////////////////////////////////
            // TUF-1.0.5 §5.4.2:
            //
            //     Check for an arbitrary software attack. The new targets metadata file MUST have
            //     been signed by a threshold of keys specified in the trusted root metadata file.
            //     If the new targets metadata file is not signed as required, discard it, abort
            //     the update cycle, and report the failure.
            let new_targets = verify::verify_signatures(
                raw_targets,
                trusted_root.targets().threshold(),
                trusted_root.targets_keys(),
            )?;
            /////////////////////////////////////////
            // FIXME(https://github.com/theupdateframework/specification/pull/112): Actually check
            // the version.
            if new_targets.version() != trusted_targets_description.version() {
                return Err(Error::VerificationFailure(format!(
                    "The timestamp metadata reported that the targets metadata should be at \
                     version {} but version {} was found instead.",
                    trusted_targets_description.version(),
                    new_targets.version()
                )));
            }
            /////////////////////////////////////////
            // TUF-1.0.5 §5.4.3:
            //
            //     Check for a freeze attack. The latest known time should be lower than the
            //     expiration timestamp in the new targets metadata file. If so, the new targets
            //     metadata file becomes the trusted targets metadata file. If the new targets
            //     metadata file is expired, discard it, abort the update cycle, and report the
            //     potential freeze attack.
            if new_targets.expires() <= &Utc::now() {
                return Err(Error::ExpiredMetadata(Role::Snapshot));
            }
            new_targets
        };
        self.trusted_targets = Some(verified);
        Ok(true)
    }
pub fn update_targets ( & mut self , raw_targets : & RawSignedMetadata < D , TargetsMetadata > , ) -> Result < bool > { let verified = { // FIXME(https://github.com/theupdateframework/specification/issues/113) Checking if // this metadata expired isn't part of the spec. Do we actually want to do this? let trusted_root = self . trusted_root_unexpired ( ) ? ; let trusted_snapshot = self . trusted_snapshot_unexpired ( ) ? ; // FIXME(#295): TUF-1.0.5 §5.3.3.2 says this check should be done when updating the // snapshot, not here. let trusted_targets_description = trusted_snapshot . meta ( ) . get ( & MetadataPath :: from_role ( & Role :: Targets ) ) . ok_or_else ( | | { Error :: VerificationFailure ( " " . into ( ) , ) } ) ? ; let trusted_targets_version = self . trusted_targets_version ( ) ; match trusted_targets_description . version ( ) . cmp ( & trusted_targets_version ) { Ordering :: Less => { return Err ( Error :: VerificationFailure ( format ! ( " " trusted_targets_version trusted_targets_description version ( ) ) ) ) ; } Ordering :: Equal => { return Ok ( false ) ; } Ordering :: Greater => { } } ///////////////////////////////////////// // TUF-1.0.5 §5.4.1: // //     Check against snapshot metadata. The hashes and version number of the new //     targets metadata file MUST match the hashes (if any) and version number listed //     in the trusted snapshot metadata. This is done, in part, to prevent a //     mix-and-match attack by man-in-the-middle attackers. If the new targets metadata //     file does not match, discard it, abort the update cycle, and report the failure. // FIXME: rust-tuf checks the hash during download, but it would be better if we // checked the hash here to make it easier to validate we've correctly implemented the // spec. // NOTE(https://github.com/theupdateframework/specification/pull/112): Technically // we're supposed to check the version before checking the signature, but we do it // afterwards. That PR proposes formally moving the version check to after signature // verification. ///////////////////////////////////////// // TUF-1.0.5 §5.4.2: // //     Check for an arbitrary software attack. The new targets metadata file MUST have //     been signed by a threshold of keys specified in the trusted root metadata file. //     If the new targets metadata file is not signed as required, discard it, abort //     the update cycle, and report the failure. let new_targets = verify :: verify_signatures ( raw_targets , trusted_root . targets ( ) . threshold ( ) , trusted_root . targets_keys ( ) , ) ? ; ///////////////////////////////////////// // FIXME(https://github.com/theupdateframework/specification/pull/112): Actually check // the version. if new_targets . version ( ) != trusted_targets_description . version ( ) { return Err ( Error :: VerificationFailure ( format ! ( " \
 " trusted_targets_description version ( ) new_targets version ( ) ) ) ) ; } ///////////////////////////////////////// // TUF-1.0.5 §5.4.3: // //     Check for a freeze attack. The latest known time should be lower than the //     expiration timestamp in the new targets metadata file. If so, the new targets //     metadata file becomes the trusted targets metadata file. If the new targets //     metadata file is expired, discard it, abort the update cycle, and report the //     potential freeze attack. if new_targets . expires ( ) <= & Utc :: now ( ) { return Err ( Error :: ExpiredMetadata ( Role :: Snapshot ) ) ; } new_targets } ; self . trusted_targets = Some ( verified ) ; Ok ( true ) }
Verify and update the targets metadata.
Verify and update the targets metadata .
&mut self
raw_targets
&RawSignedMetadata<D, TargetsMetadata>