New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pe: add authenticode support #358
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
// Reference: | ||
// https://learn.microsoft.com/en-us/windows-hardware/drivers/install/authenticode | ||
// https://download.microsoft.com/download/9/c/5/9c5b2167-8017-4bae-9fde-d599bac8184a/Authenticode_PE.docx | ||
|
||
// Authenticode works by omiting sections of the PE binary from the digest | ||
// those sections are: | ||
// - checksum | ||
// - data directory entry for certtable | ||
// - certtable | ||
|
||
use alloc::{boxed::Box, vec::Vec}; | ||
use core::ops::Range; | ||
use digest::{Digest, Output}; | ||
|
||
use super::PE; | ||
|
||
impl PE<'_> { | ||
/// [`authenticode_ranges`] returns the various ranges of the binary that are relevant for | ||
/// signature. | ||
fn authenticode_ranges(&self) -> ExcludedAuthenticodeSectionsIter<'_> { | ||
ExcludedAuthenticodeSectionsIter { | ||
pe: self, | ||
state: IterState::default(), | ||
} | ||
} | ||
|
||
/// [`authenticode_digest`] returns the result of the provided hash algorithm. | ||
pub fn authenticode_digest<D: Digest>(&self) -> Output<D> { | ||
let mut digest = D::new(); | ||
|
||
for chunk in self.authenticode_ranges() { | ||
digest.update(chunk); | ||
} | ||
|
||
digest.finalize() | ||
} | ||
|
||
/// [`authenticode_slice`] is intended for convenience when signing a binary with a PKCS#11 | ||
/// interface (HSM interface). | ||
/// Some algorithms (RSA-PKCS at least) for signature require the non-prehashed slice to be provided. | ||
pub fn authenticode_slice(&self) -> Box<[u8]> { | ||
// PE may be 70-80MB large (especially for linux UKIs). We'll get the length beforehand as | ||
// it's cheaper than getting Vec to realloc and move stuff around multiple times. | ||
let mut length = 0; | ||
for chunk in self.authenticode_ranges() { | ||
length += chunk.len(); | ||
} | ||
|
||
let mut out = Vec::with_capacity(length); | ||
for chunk in self.authenticode_ranges() { | ||
out.extend_from_slice(chunk); | ||
} | ||
|
||
out.into() | ||
} | ||
} | ||
|
||
/// [`ExcludedAuthenticodeSections`] holds the various ranges of the binary that are expected to be | ||
/// excluded from the authenticode computation. | ||
#[derive(Debug, Clone, Default)] | ||
pub(super) struct ExcludedAuthenticodeSections { | ||
pub checksum: Range<usize>, | ||
pub datadir_entry_certtable: Range<usize>, | ||
pub certtable: Option<Range<usize>>, | ||
} | ||
|
||
pub struct ExcludedAuthenticodeSectionsIter<'s> { | ||
pe: &'s PE<'s>, | ||
state: IterState, | ||
} | ||
|
||
#[derive(Default, Debug, PartialEq)] | ||
enum IterState { | ||
#[default] | ||
Initial, | ||
DatadirEntry(usize), | ||
CertTable(usize), | ||
Final(usize), | ||
Done, | ||
} | ||
|
||
impl<'s> Iterator for ExcludedAuthenticodeSectionsIter<'s> { | ||
type Item = &'s [u8]; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
let bytes = &self.pe.bytes; | ||
|
||
if let Some(sections) = self.pe.excluded_authenticode_sections.as_ref() { | ||
loop { | ||
match self.state { | ||
IterState::Initial => { | ||
self.state = IterState::DatadirEntry(sections.checksum.end); | ||
return Some(&bytes[..sections.checksum.start]); | ||
} | ||
IterState::DatadirEntry(start) => { | ||
self.state = IterState::CertTable(sections.datadir_entry_certtable.end); | ||
return Some(&bytes[start..sections.datadir_entry_certtable.start]); | ||
} | ||
IterState::CertTable(start) => { | ||
if let Some(certtable) = sections.certtable.as_ref() { | ||
self.state = IterState::Final(certtable.end); | ||
return Some(&bytes[start..certtable.start]); | ||
} else { | ||
self.state = IterState::Final(start) | ||
} | ||
} | ||
IterState::Final(start) => { | ||
self.state = IterState::Done; | ||
return Some(&bytes[start..]); | ||
} | ||
IterState::Done => return None, | ||
} | ||
} | ||
} else { | ||
loop { | ||
match self.state { | ||
IterState::Initial => { | ||
self.state = IterState::Done; | ||
return Some(bytes); | ||
} | ||
IterState::Done => return None, | ||
_ => { | ||
self.state = IterState::Done; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not so sure about this, what would be the rationale to introduce this vs. just letting the caller passing the original bytes around?
I will let @m4b weighs on as I don't have fully context on the design goals of the library.