-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Authenticode is the hashing format used to sign PE binaries. This provides the hash to be signed.
- Loading branch information
Showing
3 changed files
with
191 additions
and
3 deletions.
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,134 @@ | ||
// 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::vec::Vec; | ||
use core::ops::Range; | ||
use digest::{Digest, Output}; | ||
|
||
use super::PE; | ||
|
||
pub trait Authenticode { | ||
fn authenticode_ranges(&self) -> AuthenticodeSectionsIter; | ||
|
||
fn authenticode_digest<D: Digest>(&self) -> Output<D> { | ||
let mut digest = D::new(); | ||
|
||
for chunk in self.authenticode_ranges() { | ||
digest.update(chunk); | ||
} | ||
|
||
digest.finalize() | ||
} | ||
|
||
fn authenticode_slice(&self) -> Box<[u8]> { | ||
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() | ||
} | ||
} | ||
|
||
impl<'pe> Authenticode for PE<'pe> { | ||
fn authenticode_ranges(&self) -> AuthenticodeSectionsIter<'_> { | ||
self.authenticode_sections.iter(self.bytes) | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Default)] | ||
pub(super) struct AuthenticodeSections { | ||
pub checksum: Option<Range<usize>>, | ||
pub datadir_entry_certtable: Option<Range<usize>>, | ||
pub certtable: Option<Range<usize>>, | ||
} | ||
|
||
impl AuthenticodeSections { | ||
fn iter<'s>(&'s self, bytes: &'s [u8]) -> AuthenticodeSectionsIter<'s> { | ||
AuthenticodeSectionsIter { | ||
bytes, | ||
state: IterState::default(), | ||
sections: self, | ||
} | ||
} | ||
} | ||
|
||
pub struct AuthenticodeSectionsIter<'s> { | ||
bytes: &'s [u8], | ||
state: IterState, | ||
sections: &'s AuthenticodeSections, | ||
} | ||
|
||
#[derive(Default)] | ||
enum IterState { | ||
#[default] | ||
Initial, | ||
DatadirEntry { | ||
start: usize, | ||
}, | ||
CertTable { | ||
start: usize, | ||
}, | ||
Final { | ||
start: usize, | ||
}, | ||
Done, | ||
} | ||
|
||
impl<'s> Iterator for AuthenticodeSectionsIter<'s> { | ||
type Item = &'s [u8]; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
loop { | ||
match self.state { | ||
IterState::Initial => { | ||
if let Some(checksum) = self.sections.checksum.as_ref() { | ||
self.state = IterState::DatadirEntry { | ||
start: checksum.end, | ||
}; | ||
return Some(&self.bytes[..checksum.start]); | ||
} else { | ||
self.state = IterState::DatadirEntry { start: 0 }; | ||
} | ||
} | ||
IterState::DatadirEntry { start } => { | ||
if let Some(datadir_entry) = self.sections.datadir_entry_certtable.as_ref() { | ||
self.state = IterState::CertTable { | ||
start: datadir_entry.end, | ||
}; | ||
return Some(&self.bytes[start..datadir_entry.start]); | ||
} else { | ||
self.state = IterState::CertTable { start } | ||
} | ||
} | ||
IterState::CertTable { start } => { | ||
if let Some(certtable) = self.sections.certtable.as_ref() { | ||
self.state = IterState::Final { | ||
start: certtable.end, | ||
}; | ||
return Some(&self.bytes[start..certtable.start]); | ||
} else { | ||
self.state = IterState::Final { start } | ||
} | ||
} | ||
IterState::Final { start } => { | ||
self.state = IterState::Done; | ||
return Some(&self.bytes[start..]); | ||
} | ||
IterState::Done => return None, | ||
} | ||
} | ||
} | ||
} |
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