pub struct CodeResources { /* private fields */ }
Expand description

Represents a _CodeSignature/CodeResources XML plist.

This file/type represents a collection of file-based resources whose content is digested and captured in this file.

Implementations§

Construct an instance by parsing an XML plist.

Serialize an instance to XML.

Examples found in repository?
src/code_resources.rs (line 1349)
1348
1349
1350
    pub fn write_code_resources(&self, writer: impl Write) -> Result<(), AppleCodesignError> {
        self.resources.to_writer_xml(writer)
    }

Add a rule to this instance in the <rules> section.

Examples found in repository?
src/code_resources.rs (line 1060)
1057
1058
1059
1060
1061
    pub fn add_rule(&mut self, rule: CodeResourcesRule) {
        self.rules.push(rule.clone());
        self.rules.sort();
        self.resources.add_rule(rule);
    }

Add a rule to this instance in the <rules2> section.

Examples found in repository?
src/code_resources.rs (line 1067)
1064
1065
1066
1067
1068
    pub fn add_rule2(&mut self, rule: CodeResourcesRule) {
        self.rules2.push(rule.clone());
        self.rules2.sort();
        self.resources.add_rule2(rule);
    }

Seal a regular file.

This will digest the content specified and record that digest in the files or files2 list.

To seal a symlink, call CodeResources::seal_symlink instead. If the file is a Mach-O file, call CodeResources::seal_macho instead.

Examples found in repository?
src/code_resources.rs (line 1246)
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
    fn process_file_rules2(
        &mut self,
        file: &DirectoryBundleFile,
        file_handler: &dyn BundleFileHandler,
    ) -> Result<(), AppleCodesignError> {
        match Self::evaluate_rules(
            &self.rules2,
            file.relative_path(),
            file.symlink_target()
                .map_err(AppleCodesignError::DirectoryBundle)?,
        )? {
            RulesEvaluation::Exclude => {
                // Excluded files are hard ignored. These files are likely handled out-of-band
                // from this builder.
                Ok(())
            }
            RulesEvaluation::Omit => {
                // Omitted files aren't sealed. But they are installed.
                file_handler.install_file(file)
            }
            RulesEvaluation::NoRule => {
                // No rule match is assumed to mean full ignore.
                Ok(())
            }
            RulesEvaluation::SealSymlink(relative_path, target) => {
                info!("sealing symlink {} -> {}", relative_path, target);
                self.resources.seal_symlink(relative_path, target);
                file_handler.install_file(file)
            }
            RulesEvaluation::SealNested(relative_path, optional) => {
                // The assumption that a nested match means Mach-O may not be correct.
                info!("sealing Mach-O file {}", relative_path);
                let macho_info = file_handler.sign_and_install_macho(file)?;

                self.resources
                    .seal_macho(relative_path, &macho_info, optional)
            }
            RulesEvaluation::SealRegularFile(relative_path, optional) => {
                info!("sealing regular file {}", relative_path);
                let data = std::fs::read(file.absolute_path())?;

                let flavor = if self.digests.contains(&DigestType::Sha1) {
                    FilesFlavor::Rules2WithSha1
                } else {
                    FilesFlavor::Rules2
                };

                self.resources
                    .seal_regular_file(flavor, relative_path, data, optional)?;
                file_handler.install_file(file)
            }
        }
    }

    /// Process the `<rules>` set for a given file.
    ///
    /// Since `<rules2>` handling actually does the file installs, the only role of this
    /// handler is to record the SHA-1 seals in `<files>`. Keep in mind that `<files>` can't
    /// handle symlinks or nested Mach-O binaries. So we only care about regular files here.
    fn process_file_rules(&mut self, file: &DirectoryBundleFile) -> Result<(), AppleCodesignError> {
        match Self::evaluate_rules(
            &self.rules,
            file.relative_path(),
            file.symlink_target()
                .map_err(AppleCodesignError::DirectoryBundle)?,
        )? {
            RulesEvaluation::Exclude
            | RulesEvaluation::Omit
            | RulesEvaluation::NoRule
            | RulesEvaluation::SealSymlink(..)
            | RulesEvaluation::SealNested(..) => Ok(()),
            RulesEvaluation::SealRegularFile(relative_path, optional) => {
                let data = std::fs::read(file.absolute_path())?;

                self.resources
                    .seal_regular_file(FilesFlavor::Rules, relative_path, data, optional)
            }
        }
    }

Seal a symlink file.

path is the path of the symlink and target is the path it points to.

Examples found in repository?
src/code_resources.rs (line 1224)
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
    fn process_file_rules2(
        &mut self,
        file: &DirectoryBundleFile,
        file_handler: &dyn BundleFileHandler,
    ) -> Result<(), AppleCodesignError> {
        match Self::evaluate_rules(
            &self.rules2,
            file.relative_path(),
            file.symlink_target()
                .map_err(AppleCodesignError::DirectoryBundle)?,
        )? {
            RulesEvaluation::Exclude => {
                // Excluded files are hard ignored. These files are likely handled out-of-band
                // from this builder.
                Ok(())
            }
            RulesEvaluation::Omit => {
                // Omitted files aren't sealed. But they are installed.
                file_handler.install_file(file)
            }
            RulesEvaluation::NoRule => {
                // No rule match is assumed to mean full ignore.
                Ok(())
            }
            RulesEvaluation::SealSymlink(relative_path, target) => {
                info!("sealing symlink {} -> {}", relative_path, target);
                self.resources.seal_symlink(relative_path, target);
                file_handler.install_file(file)
            }
            RulesEvaluation::SealNested(relative_path, optional) => {
                // The assumption that a nested match means Mach-O may not be correct.
                info!("sealing Mach-O file {}", relative_path);
                let macho_info = file_handler.sign_and_install_macho(file)?;

                self.resources
                    .seal_macho(relative_path, &macho_info, optional)
            }
            RulesEvaluation::SealRegularFile(relative_path, optional) => {
                info!("sealing regular file {}", relative_path);
                let data = std::fs::read(file.absolute_path())?;

                let flavor = if self.digests.contains(&DigestType::Sha1) {
                    FilesFlavor::Rules2WithSha1
                } else {
                    FilesFlavor::Rules2
                };

                self.resources
                    .seal_regular_file(flavor, relative_path, data, optional)?;
                file_handler.install_file(file)
            }
        }
    }

Record metadata of a previously signed Mach-O binary.

If sealing a fat/universal binary, pass in metadata for the first Mach-O within in.

Examples found in repository?
src/code_resources.rs (line 1233)
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
    fn process_file_rules2(
        &mut self,
        file: &DirectoryBundleFile,
        file_handler: &dyn BundleFileHandler,
    ) -> Result<(), AppleCodesignError> {
        match Self::evaluate_rules(
            &self.rules2,
            file.relative_path(),
            file.symlink_target()
                .map_err(AppleCodesignError::DirectoryBundle)?,
        )? {
            RulesEvaluation::Exclude => {
                // Excluded files are hard ignored. These files are likely handled out-of-band
                // from this builder.
                Ok(())
            }
            RulesEvaluation::Omit => {
                // Omitted files aren't sealed. But they are installed.
                file_handler.install_file(file)
            }
            RulesEvaluation::NoRule => {
                // No rule match is assumed to mean full ignore.
                Ok(())
            }
            RulesEvaluation::SealSymlink(relative_path, target) => {
                info!("sealing symlink {} -> {}", relative_path, target);
                self.resources.seal_symlink(relative_path, target);
                file_handler.install_file(file)
            }
            RulesEvaluation::SealNested(relative_path, optional) => {
                // The assumption that a nested match means Mach-O may not be correct.
                info!("sealing Mach-O file {}", relative_path);
                let macho_info = file_handler.sign_and_install_macho(file)?;

                self.resources
                    .seal_macho(relative_path, &macho_info, optional)
            }
            RulesEvaluation::SealRegularFile(relative_path, optional) => {
                info!("sealing regular file {}", relative_path);
                let data = std::fs::read(file.absolute_path())?;

                let flavor = if self.digests.contains(&DigestType::Sha1) {
                    FilesFlavor::Rules2WithSha1
                } else {
                    FilesFlavor::Rules2
                };

                self.resources
                    .seal_regular_file(flavor, relative_path, data, optional)?;
                file_handler.install_file(file)
            }
        }
    }

    /// Process the `<rules>` set for a given file.
    ///
    /// Since `<rules2>` handling actually does the file installs, the only role of this
    /// handler is to record the SHA-1 seals in `<files>`. Keep in mind that `<files>` can't
    /// handle symlinks or nested Mach-O binaries. So we only care about regular files here.
    fn process_file_rules(&mut self, file: &DirectoryBundleFile) -> Result<(), AppleCodesignError> {
        match Self::evaluate_rules(
            &self.rules,
            file.relative_path(),
            file.symlink_target()
                .map_err(AppleCodesignError::DirectoryBundle)?,
        )? {
            RulesEvaluation::Exclude
            | RulesEvaluation::Omit
            | RulesEvaluation::NoRule
            | RulesEvaluation::SealSymlink(..)
            | RulesEvaluation::SealNested(..) => Ok(()),
            RulesEvaluation::SealRegularFile(relative_path, optional) => {
                let data = std::fs::read(file.absolute_path())?;

                self.resources
                    .seal_regular_file(FilesFlavor::Rules, relative_path, data, optional)
            }
        }
    }

    /// Process a file for resource handling.
    ///
    /// This determines whether a file is relevant for inclusion in the CodeResources
    /// file and takes actions to process it, if necessary.
    pub fn process_file(
        &mut self,
        file: &DirectoryBundleFile,
        file_handler: &dyn BundleFileHandler,
    ) -> Result<(), AppleCodesignError> {
        self.process_file_rules2(file, file_handler)?;
        self.process_file_rules(file)
    }

    /// Process a nested bundle for inclusion in resource handling.
    ///
    /// This will attempt to seal the main digest of the bundle into this resources file.
    pub fn process_nested_bundle(
        &mut self,
        relative_path: &str,
        bundle: &DirectoryBundle,
    ) -> Result<(), AppleCodesignError> {
        let main_exe = match bundle
            .files(false)
            .map_err(AppleCodesignError::DirectoryBundle)?
            .into_iter()
            .find(|file| matches!(file.is_main_executable(), Ok(true)))
        {
            Some(path) => path,
            None => {
                warn!(
                    "nested bundle at {} does not have main executable; nothing to seal",
                    relative_path
                );
                return Ok(());
            }
        };

        let (relative_path, optional) =
            match Self::evaluate_rules(&self.rules2, relative_path, None)? {
                RulesEvaluation::SealRegularFile(relative_path, optional) => {
                    (relative_path, optional)
                }
                RulesEvaluation::SealNested(relative_path, optional) => (relative_path, optional),
                RulesEvaluation::Exclude => {
                    info!(
                        "excluding signing nested bundle {} because of matched resources rule",
                        relative_path
                    );
                    return Ok(());
                }
                res => {
                    warn!(
                        "unexpected resource rules evaluation result for nested bundle {}: {:?}",
                        relative_path, res
                    );
                    return Err(AppleCodesignError::BundleUnexpectedResourceRuleResult);
                }
            };

        let macho_data = std::fs::read(main_exe.absolute_path())?;
        let macho_info = SignedMachOInfo::parse_data(&macho_data)?;

        info!("sealing nested bundle at {}", relative_path);
        self.resources
            .seal_macho(relative_path, &macho_info, optional)?;

        Ok(())
    }

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Converts to this type from the input type.
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Converts self into T using Into<T>. Read more
Causes self to use its Binary implementation when Debug-formatted.
Causes self to use its Display implementation when Debug-formatted.
Causes self to use its LowerExp implementation when Debug-formatted.
Causes self to use its LowerHex implementation when Debug-formatted.
Causes self to use its Octal implementation when Debug-formatted.
Causes self to use its Pointer implementation when Debug-formatted.
Causes self to use its UpperExp implementation when Debug-formatted.
Causes self to use its UpperHex implementation when Debug-formatted.
Formats each item in a sequence. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Pipes by value. This is generally the method you want to use. Read more
Borrows self and passes that borrow into the pipe function. Read more
Mutably borrows self and passes that borrow into the pipe function. Read more
Borrows self, then passes self.borrow() into the pipe function. Read more
Mutably borrows self, then passes self.borrow_mut() into the pipe function. Read more
Borrows self, then passes self.as_ref() into the pipe function.
Mutably borrows self, then passes self.as_mut() into the pipe function.
Borrows self, then passes self.deref() into the pipe function.
Mutably borrows self, then passes self.deref_mut() into the pipe function.
The alignment of pointer.
The type for initializers.
Initializes a with the given initializer. Read more
Dereferences the given pointer. Read more
Mutably dereferences the given pointer. Read more
Drops the object pointed to by the given pointer. Read more
Should always be Self
Immutable access to a value. Read more
Mutable access to a value. Read more
Immutable access to the Borrow<B> of a value. Read more
Mutable access to the BorrowMut<B> of a value. Read more
Immutable access to the AsRef<R> view of a value. Read more
Mutable access to the AsMut<R> view of a value. Read more
Immutable access to the Deref::Target of a value. Read more
Mutable access to the Deref::Target of a value. Read more
Calls .tap() only in debug builds, and is erased in release builds.
Calls .tap_mut() only in debug builds, and is erased in release builds.
Calls .tap_borrow() only in debug builds, and is erased in release builds.
Calls .tap_borrow_mut() only in debug builds, and is erased in release builds.
Calls .tap_ref() only in debug builds, and is erased in release builds.
Calls .tap_ref_mut() only in debug builds, and is erased in release builds.
Calls .tap_deref() only in debug builds, and is erased in release builds.
Calls .tap_deref_mut() only in debug builds, and is erased in release builds.
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Attempts to convert self into T using TryInto<T>. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more