Skip to content

Commit

Permalink
apple-codesign: zip notarization support
Browse files Browse the repository at this point in the history
Adds the ability to notarize .zip files containing other notarizable
assets (such as a Mach-O binary).

Stapling (via the --staple option) is not supported for .zip files and
will return an error.
  • Loading branch information
deansheather authored and indygreg committed Sep 21, 2022
1 parent 8c9a7a8 commit 66b57c8
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
3 changes: 2 additions & 1 deletion apple-codesign/src/notarization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ impl Notarizer {
self.notarize_bundle(&bundle, wait_limit)
}
PathType::Xar => self.notarize_flat_package(path, wait_limit),
PathType::Zip => self.notarize_flat_package(path, wait_limit),
PathType::Dmg => self.notarize_dmg(path, wait_limit),
PathType::MachO | PathType::Other => Err(AppleCodesignError::NotarizeUnsupportedPath(
path.to_path_buf(),
Expand Down Expand Up @@ -248,7 +249,7 @@ impl Notarizer {
)
}

/// Attempt to notarize a flat package (`.pkg`) installer.
/// Attempt to notarize a flat package (`.pkg`) installer or a .zip file.
pub fn notarize_flat_package(
&self,
pkg_path: &Path,
Expand Down
19 changes: 18 additions & 1 deletion apple-codesign/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@ pub fn path_is_xar(path: impl AsRef<Path>) -> Result<bool, AppleCodesignError> {
}
}

/// Test whether a given path is likely a ZIP file.
pub fn path_is_zip(path: impl AsRef<Path>) -> Result<bool, AppleCodesignError> {
let mut fh = File::open(path.as_ref())?;

let mut header = [0u8; 4];

let count = fh.read(&mut header)?;
if count < 4 {
Ok(false)
} else {
Ok(header.as_ref() == [0x50, 0x4b, 0x03, 0x04])
}
}

/// Describes the type of entity at a path.
///
/// This represents a best guess.
Expand All @@ -82,6 +96,7 @@ pub enum PathType {
Dmg,
Bundle,
Xar,
Zip,
Other,
}

Expand All @@ -95,6 +110,8 @@ impl PathType {
Ok(PathType::Dmg)
} else if path_is_xar(path)? {
Ok(PathType::Xar)
} else if path_is_zip(path)? {
Ok(PathType::Zip)
} else {
match MachOType::from_path(path)? {
Some(MachOType::Mach | MachOType::MachO) => Ok(Self::MachO),
Expand Down Expand Up @@ -803,7 +820,7 @@ impl SignatureReader {
Ok(Self::MachO(path.to_path_buf(), data))
}
PathType::Xar => Ok(Self::FlatPackage(path.to_path_buf())),
PathType::Other => Err(AppleCodesignError::UnrecognizedPathType),
PathType::Zip | PathType::Other => Err(AppleCodesignError::UnrecognizedPathType),
}
}

Expand Down
2 changes: 1 addition & 1 deletion apple-codesign/src/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl<'key> UnifiedSigner<'key> {
PathType::Dmg => self.sign_dmg(input_path, output_path),
PathType::MachO => self.sign_macho(input_path, output_path),
PathType::Xar => self.sign_xar(input_path, output_path),
PathType::Other => Err(AppleCodesignError::UnrecognizedPathType),
PathType::Zip | PathType::Other => Err(AppleCodesignError::UnrecognizedPathType),
}
}

Expand Down
2 changes: 1 addition & 1 deletion apple-codesign/src/stapling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ impl Stapler {
let xar = XarReader::new(File::options().read(true).write(true).open(path)?)?;
self.staple_xar(xar)
}
PathType::Other => Err(AppleCodesignError::StapleUnsupportedPath(
PathType::Zip | PathType::Other => Err(AppleCodesignError::StapleUnsupportedPath(
path.to_path_buf(),
)),
}
Expand Down

0 comments on commit 66b57c8

Please sign in to comment.