| 1 |
/// Implements parsing of pe32's Attribute Certificate Table |
| 2 |
/// See reference: |
| 3 |
/// https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#the-attribute-certificate-table-image-only |
| 4 |
/// https://learn.microsoft.com/en-us/windows/win32/api/wintrust/ns-wintrust-win_certificate |
| 5 |
use crate::error; |
| 6 |
use scroll::{ctx, Pread, Pwrite}; |
| 7 |
|
| 8 |
use alloc::string::ToString; |
| 9 |
use alloc::vec::Vec; |
| 10 |
|
| 11 |
use super::utils::pad; |
| 12 |
|
| 13 |
#[repr(u16)] |
| 14 |
#[non_exhaustive] |
| 15 |
#[derive(Debug, PartialEq, Copy, Clone)] |
| 16 |
pub enum AttributeCertificateRevision { |
| 17 |
/// WIN_CERT_REVISION_1_0 |
| 18 |
Revision1_0 = 0x0100, |
| 19 |
/// WIN_CERT_REVISION_2_0 |
| 20 |
Revision2_0 = 0x0200, |
| 21 |
} |
| 22 |
|
| 23 |
impl TryFrom<u16> for AttributeCertificateRevision { |
| 24 |
type Error = error::Error; |
| 25 |
|
| 26 |
fn try_from(value: u16) -> Result<Self, Self::Error> { |
| 27 |
Ok(match value { |
| 28 |
x if x == AttributeCertificateRevision::Revision1_0 as u16 => { |
| 29 |
AttributeCertificateRevision::Revision1_0 |
| 30 |
} |
| 31 |
x if x == AttributeCertificateRevision::Revision2_0 as u16 => { |
| 32 |
AttributeCertificateRevision::Revision2_0 |
| 33 |
} |
| 34 |
_ => { |
| 35 |
return Err(error::Error::Malformed( |
| 36 |
"Invalid certificate attribute revision".to_string(), |
| 37 |
)) |
| 38 |
} |
| 39 |
}) |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
#[repr(u16)] |
| 44 |
#[derive(Debug, PartialEq, Copy, Clone)] |
| 45 |
pub enum AttributeCertificateType { |
| 46 |
/// WIN_CERT_TYPE_X509 |
| 47 |
X509 = 0x0001, |
| 48 |
/// WIN_CERT_TYPE_PKCS_SIGNED_DATA |
| 49 |
PkcsSignedData = 0x0002, |
| 50 |
/// WIN_CERT_TYPE_RESERVED_1 |
| 51 |
Reserved1 = 0x0003, |
| 52 |
/// WIN_CERT_TYPE_TS_STACK_SIGNED |
| 53 |
TsStackSigned = 0x0004, |
| 54 |
} |
| 55 |
|
| 56 |
impl TryFrom<u16> for AttributeCertificateType { |
| 57 |
type Error = error::Error; |
| 58 |
|
| 59 |
fn try_from(value: u16) -> Result<Self, Self::Error> { |
| 60 |
Ok(match value { |
| 61 |
x if x == AttributeCertificateType::X509 as u16 => AttributeCertificateType::X509, |
| 62 |
x if x == AttributeCertificateType::PkcsSignedData as u16 => { |
| 63 |
AttributeCertificateType::PkcsSignedData |
| 64 |
} |
| 65 |
x if x == AttributeCertificateType::Reserved1 as u16 => { |
| 66 |
AttributeCertificateType::Reserved1 |
| 67 |
} |
| 68 |
x if x == AttributeCertificateType::TsStackSigned as u16 => { |
| 69 |
AttributeCertificateType::TsStackSigned |
| 70 |
} |
| 71 |
_ => { |
| 72 |
return Err(error::Error::Malformed( |
| 73 |
"Invalid attribute certificate type".to_string(), |
| 74 |
)) |
| 75 |
} |
| 76 |
}) |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
#[derive(Clone, Pread)] |
| 81 |
struct AttributeCertificateHeader { |
| 82 |
/// dwLength |
| 83 |
length: u32, |
| 84 |
revision: u16, |
| 85 |
certificate_type: u16, |
| 86 |
} |
| 87 |
|
| 88 |
const CERTIFICATE_DATA_OFFSET: u32 = 8; |
| 89 |
#[derive(Debug)] |
| 90 |
pub struct AttributeCertificate<'a> { |
| 91 |
pub length: u32, |
| 92 |
pub revision: AttributeCertificateRevision, |
| 93 |
pub certificate_type: AttributeCertificateType, |
| 94 |
pub certificate: &'a [u8], |
| 95 |
} |
| 96 |
|
| 97 |
impl<'a> AttributeCertificate<'a> { |
| 98 |
pub fn parse( |
| 99 |
bytes: &'a [u8], |
| 100 |
current_offset: &mut usize, |
| 101 |
) -> Result<AttributeCertificate<'a>, error::Error> { |
| 102 |
// `current_offset` is moved sizeof(AttributeCertificateHeader) = 8 bytes further. |
| 103 |
let header: AttributeCertificateHeader = bytes.gread_with(current_offset, scroll::LE)?; |
| 104 |
let cert_size = usize::try_from(header.length.saturating_sub(CERTIFICATE_DATA_OFFSET)) |
| 105 |
.map_err(|_err| { |
| 106 |
error::Error::Malformed( |
| 107 |
"Attribute certificate size do not fit in usize".to_string(), |
| 108 |
) |
| 109 |
})?; |
| 110 |
|
| 111 |
if let Some(bytes) = bytes.get(*current_offset..(*current_offset + cert_size)) { |
| 112 |
let attr = Self { |
| 113 |
length: header.length, |
| 114 |
revision: header.revision.try_into()?, |
| 115 |
certificate_type: header.certificate_type.try_into()?, |
| 116 |
certificate: bytes, |
| 117 |
}; |
| 118 |
// Moving past the certificate data. |
| 119 |
// Prevent the current_offset to wrap and ensure current_offset is strictly increasing. |
| 120 |
*current_offset = current_offset.saturating_add(cert_size); |
| 121 |
// Round to the next 8-bytes. |
| 122 |
*current_offset = (*current_offset + 7) & !7; |
| 123 |
Ok(attr) |
| 124 |
} else { |
| 125 |
Err(error::Error::Malformed(format!( |
| 126 |
"Unable to extract certificate. Probably cert_size:{} is malformed", |
| 127 |
cert_size |
| 128 |
))) |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
impl<'a> ctx::TryIntoCtx<scroll::Endian> for &AttributeCertificate<'a> { |
| 134 |
type Error = error::Error; |
| 135 |
|
| 136 |
/// Writes an aligned attribute certificate in the buffer. |
| 137 |
fn try_into_ctx(self, bytes: &mut [u8], ctx: scroll::Endian) -> Result<usize, Self::Error> { |
| 138 |
let offset = &mut 0; |
| 139 |
bytes.gwrite_with(self.length, offset, ctx)?; |
| 140 |
bytes.gwrite_with(self.revision as u16, offset, ctx)?; |
| 141 |
bytes.gwrite_with(self.certificate_type as u16, offset, ctx)?; |
| 142 |
// Extend by zero the buffer until it is aligned on a quadword (16 bytes). |
| 143 |
let maybe_certificate_padding = pad(self.certificate.len(), Some(16usize)); |
| 144 |
bytes.gwrite(self.certificate, offset)?; |
| 145 |
if let Some(cert_padding) = maybe_certificate_padding { |
| 146 |
bytes.gwrite(&cert_padding[..], offset)?; |
| 147 |
} |
| 148 |
|
| 149 |
Ok(*offset) |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
pub type CertificateDirectoryTable<'a> = Vec<AttributeCertificate<'a>>; |
| 154 |
|
| 155 |
pub(crate) fn enumerate_certificates( |
| 156 |
bytes: &[u8], |
| 157 |
table_virtual_address: u32, |
| 158 |
table_size: u32, |
| 159 |
) -> Result<CertificateDirectoryTable, error::Error> { |
| 160 |
let table_start_offset = usize::try_from(table_virtual_address).map_err(|_err| { |
| 161 |
error::Error::Malformed("Certificate table RVA do not fit in a usize".to_string()) |
| 162 |
})?; |
| 163 |
// Here, we do not want wrapping semantics as it means that a too big table size or table start |
| 164 |
// offset will provide table_end_offset such that table_end_offset < table_start_offset, which |
| 165 |
// is not desirable at all. |
| 166 |
let table_end_offset = |
| 167 |
table_start_offset.saturating_add(usize::try_from(table_size).map_err(|_err| { |
| 168 |
error::Error::Malformed("Certificate table size do not fit in a usize".to_string()) |
| 169 |
})?); |
| 170 |
let mut current_offset = table_start_offset; |
| 171 |
let mut attrs = vec![]; |
| 172 |
|
| 173 |
// End offset cannot be further than the binary we have at hand. |
| 174 |
if table_end_offset > bytes.len() { |
| 175 |
return Err(error::Error::Malformed( |
| 176 |
"End of attribute certificates table is after the end of the PE binary".to_string(), |
| 177 |
)); |
| 178 |
} |
| 179 |
|
| 180 |
// This is guaranteed to terminate, either by a malformed error being returned |
| 181 |
// or because current_offset >= table_end_offset by virtue of current_offset being strictly |
| 182 |
// increasing through `AttributeCertificate::parse`. |
| 183 |
while current_offset < table_end_offset { |
| 184 |
attrs.push(AttributeCertificate::parse(bytes, &mut current_offset)?); |
| 185 |
} |
| 186 |
|
| 187 |
Ok(attrs) |
| 188 |
} |