wintrust 0.3.0

Portable Rust catalog trust with Windows API name aliases
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
//! Portable, explicit catalog member hashing. Unknown SIP formats fail closed.
use crate::catalog::Catalog;
use crate::error::{Context, Result, ensure};
use alloc::{string::String, vec, vec::Vec};
use der::asn1::ObjectIdentifier;
use serde::{Deserialize, Serialize};
use sha2::Digest;

/// Explicit member format; no extension-based or unknown-format fallback.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SipKind {
    Pe,
    FlatXml,
    FlatRaw,
    Cab,
}
/// Supported digest identifiers; SHA-1 requires explicit compatibility policy.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum DigestAlgorithm {
    Sha1,
    Sha256,
    Sha384,
    Sha512,
}
impl DigestAlgorithm {
    pub fn from_oid(oid: ObjectIdentifier) -> Result<Self> {
        [Self::Sha1, Self::Sha256, Self::Sha384, Self::Sha512]
            .into_iter()
            .find(|algorithm| algorithm.oid() == oid)
            .ok_or_else(|| {
                crate::error::Error::unsupported(alloc::format!("member digest algorithm {oid}"))
            })
    }
    pub fn oid(self) -> ObjectIdentifier {
        match self {
            Self::Sha1 => ObjectIdentifier::new_unwrap("1.3.14.3.2.26"),
            Self::Sha256 => ObjectIdentifier::new_unwrap("2.16.840.1.101.3.4.2.1"),
            Self::Sha384 => ObjectIdentifier::new_unwrap("2.16.840.1.101.3.4.2.2"),
            Self::Sha512 => ObjectIdentifier::new_unwrap("2.16.840.1.101.3.4.2.3"),
        }
    }
}
/// Resource and legacy-algorithm policy, independent of signature trust.
#[derive(Debug, Clone)]
pub struct MemberHashPolicy {
    pub allow_sha1: bool,
    pub max_member_bytes: usize,
}
impl Default for MemberHashPolicy {
    fn default() -> Self {
        Self {
            allow_sha1: false,
            max_member_bytes: 256 * 1024 * 1024,
        }
    }
}
fn u16_at(bytes: &[u8], offset: usize) -> Result<u16> {
    hexspell::utils::extract_u16(bytes, offset).context("truncated member field")
}
fn u32_at(bytes: &[u8], offset: usize) -> Result<u32> {
    hexspell::utils::extract_u32(bytes, offset).context("truncated member field")
}
fn range(bytes: &[u8], start: usize, length: usize) -> Result<&[u8]> {
    bytes
        .get(start..start.checked_add(length).context("member range overflow")?)
        .context("member range outside file")
}

fn pe_chunks(bytes: &[u8]) -> Result<Vec<&[u8]>> {
    let parsed = hexspell::pe::view::PeHeaders::parse(bytes).context("invalid PE headers")?;
    let pe = parsed.nt;
    ensure!(
        pe >= 0x40,
        crate::error::Error::malformed("invalid PE signature")
    );
    let sections = usize::from(parsed.coff.number_of_sections.value);
    ensure!(sections <= 4096, "excessive PE section count");
    let optional_length = usize::from(parsed.coff.size_of_optional_header.value);
    let optional = parsed.optional;
    range(bytes, optional, optional_length)?;
    let directory = parsed
        .directory_offset()
        .context("unsupported PE optional header")?;
    let count_offset = directory - 4;
    ensure!(
        optional_length >= directory,
        crate::error::Error::malformed("truncated PE optional header")
    );
    let headers = u32_at(bytes, optional + 60)? as usize;
    let checksum = optional + 64;
    let count = u32_at(bytes, optional + count_offset)? as usize;
    ensure!(
        count <= (optional_length - directory) / 8,
        "PE directory count exceeds optional header"
    );
    let security = optional + directory + 4 * 8;
    let (certificate, certificate_size) = if count > 4 {
        let (offset, size) = parsed
            .directory_slot(4)
            .map_err(crate::error::Error::malformed)?;
        (offset as usize, size as usize)
    } else {
        (0, 0)
    };
    ensure!(
        (certificate == 0) == (certificate_size == 0),
        "incomplete PE certificate table range"
    );
    let section_table = optional + optional_length;
    let section_table_end = section_table
        .checked_add(sections * 40)
        .context("PE section table overflow")?;
    ensure!(
        headers >= section_table_end && headers <= bytes.len(),
        crate::error::Error::malformed("invalid PE header size")
    );
    ensure!(checksum + 4 <= headers, "PE checksum lies outside headers");
    let mut chunks = vec![range(bytes, 0, checksum)?];
    if count > 4 {
        ensure!(
            security >= checksum + 4 && security + 8 <= headers,
            "PE security directory lies outside headers"
        );
        chunks.push(range(bytes, checksum + 4, security - checksum - 4)?);
        chunks.push(range(bytes, security + 8, headers - security - 8)?);
    } else {
        chunks.push(range(bytes, checksum + 4, headers - checksum - 4)?);
    }
    let mut raw = Vec::new();
    for index in 0..sections {
        let section = parsed.section(index).context("invalid PE section header")?;
        let size = section.size_of_raw_data.value as usize;
        let start = section.pointer_to_raw_data.value as usize;
        if size > 0 {
            ensure!(start >= headers, "PE section overlaps headers");
            range(bytes, start, size)?;
            raw.push((start, size));
        }
    }
    raw.sort_unstable();
    let mut last = headers;
    let mut sum = headers;
    for (start, size) in raw {
        ensure!(start >= last, "overlapping PE raw sections");
        last = start.checked_add(size).context("PE section end overflow")?;
        sum = sum.checked_add(size).context("PE hashed size overflow")?;
        chunks.push(range(bytes, start, size)?);
    }
    let end = if certificate_size > 0 {
        ensure!(
            certificate % 8 == 0
                && certificate >= last
                && certificate.checked_add(certificate_size) == Some(bytes.len()),
            crate::error::Error::unsupported(
                "unsupported nonterminal or overlapping PE certificate table"
            )
        );
        let mut cursor = certificate;
        while cursor < bytes.len() {
            let length = u32_at(bytes, cursor)? as usize;
            ensure!(
                length >= 8,
                crate::error::Error::malformed("invalid WIN_CERTIFICATE length")
            );
            let rounded = length
                .checked_add(7)
                .context("certificate length overflow")?
                & !7;
            cursor = cursor
                .checked_add(rounded)
                .context("certificate offset overflow")?;
            ensure!(
                cursor <= bytes.len(),
                crate::error::Error::malformed("truncated WIN_CERTIFICATE")
            );
        }
        certificate
    } else {
        bytes.len()
    };
    // Authenticode's extra-data rule starts at SizeOfHeaders + sum(raw sizes),
    // not at the largest section end. This matters for gapped section layouts.
    ensure!(sum <= end, "PE sum of hashed bytes exceeds payload extent");
    if end > sum {
        chunks.push(range(bytes, sum, end - sum)?);
    }
    Ok(chunks)
}

fn flat_xml(bytes: &[u8]) -> Result<()> {
    let text = core::str::from_utf8(bytes).context("flat XML member is not UTF-8")?;
    let xml = roxmltree::Document::parse(text)
        .map_err(crate::error::Error::malformed)
        .context("invalid flat XML member")?;
    ensure!(
        xml.root_element().tag_name().name() == "assembly",
        "flat XML member must be an assembly MUM/manifest"
    );
    Ok(())
}
fn flat_raw(bytes: &[u8]) -> Result<()> {
    for magic in [
        b"MZ".as_slice(),
        b"MSCF",
        b"PK\x03\x04",
        b"\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1",
        b"MSWIM\0\0\0",
    ] {
        ensure!(
            !bytes.starts_with(magic),
            "flat raw member resembles a format requiring a dedicated SIP"
        );
    }
    Ok(())
}

/// Compute a catalog-member digest. This does not verify a catalog signature.
pub fn member_hash(
    bytes: &[u8],
    kind: SipKind,
    algorithm: DigestAlgorithm,
    policy: &MemberHashPolicy,
) -> Result<String> {
    Ok(hex::encode(member_hash_bytes(
        bytes, kind, algorithm, policy,
    )?))
}

/// Compute raw catalog-member digest bytes without a hex encoding round trip.
pub fn member_hash_bytes(
    bytes: &[u8],
    kind: SipKind,
    algorithm: DigestAlgorithm,
    policy: &MemberHashPolicy,
) -> Result<Vec<u8>> {
    ensure!(
        bytes.len() <= policy.max_member_bytes,
        crate::error::Error::resource_limit("catalog member exceeds byte limit")
    );
    ensure!(
        algorithm != DigestAlgorithm::Sha1 || policy.allow_sha1,
        "SHA-1 member digest requires explicit compatibility policy"
    );
    let chunks = match kind {
        SipKind::Pe => pe_chunks(bytes)?,
        SipKind::FlatXml => {
            flat_xml(bytes)?;
            vec![bytes]
        }
        SipKind::FlatRaw => {
            flat_raw(bytes)?;
            vec![bytes]
        }
        SipKind::Cab => cab_chunks(bytes)?,
    };
    macro_rules! hash {
        ($algorithm:ty) => {{
            let mut hash = <$algorithm>::new();
            for chunk in chunks {
                hash.update(chunk);
            }
            hash.finalize().to_vec()
        }};
    }
    Ok(match algorithm {
        DigestAlgorithm::Sha1 => hash!(sha1::Sha1),
        DigestAlgorithm::Sha256 => hash!(sha2::Sha256),
        DigestAlgorithm::Sha384 => hash!(sha2::Sha384),
        DigestAlgorithm::Sha512 => hash!(sha2::Sha512),
    })
}

/// Return member indices whose declared data type and digest match this format.
/// Unknown algorithms/types are not treated as flat hashes.
pub fn match_catalog_member(
    catalog: &Catalog,
    bytes: &[u8],
    kind: SipKind,
    policy: &MemberHashPolicy,
) -> Result<Vec<usize>> {
    ensure!(
        bytes.len() <= policy.max_member_bytes,
        crate::error::Error::resource_limit("catalog member exceeds byte limit")
    );
    let mut matched = Vec::new();
    let mut hashes: [Option<String>; 4] = [None, None, None, None];
    for (index, member) in catalog.ctl.members.iter().enumerate() {
        let Some(indirect) = &member.indirect_data else {
            continue;
        };
        let compatible = match kind {
            SipKind::Pe => {
                indirect.data_type == ObjectIdentifier::new_unwrap("1.3.6.1.4.1.311.2.1.15")
            }
            SipKind::FlatXml | SipKind::FlatRaw => [
                ObjectIdentifier::new_unwrap("1.3.6.1.4.1.311.2.1.18"),
                ObjectIdentifier::new_unwrap("1.3.6.1.4.1.311.2.1.25"),
            ]
            .contains(&indirect.data_type),
            SipKind::Cab => {
                indirect.data_type == ObjectIdentifier::new_unwrap("1.3.6.1.4.1.311.2.1.25")
            }
        };
        if !compatible {
            continue;
        }
        let Ok(algorithm) = DigestAlgorithm::from_oid(indirect.digest_algorithm) else {
            continue;
        };
        if algorithm == DigestAlgorithm::Sha1 && !policy.allow_sha1 {
            continue;
        }
        let cache_index = match algorithm {
            DigestAlgorithm::Sha1 => 0,
            DigestAlgorithm::Sha256 => 1,
            DigestAlgorithm::Sha384 => 2,
            DigestAlgorithm::Sha512 => 3,
        };
        if hashes[cache_index].is_none() {
            hashes[cache_index] = Some(member_hash(bytes, kind, algorithm, policy)?);
        }
        if hashes[cache_index]
            .as_deref()
            .is_some_and(|hash| hash.eq_ignore_ascii_case(&indirect.digest_hex))
        {
            matched.push(index);
        }
    }
    Ok(matched)
}

fn cab_chunks(bytes: &[u8]) -> Result<Vec<&[u8]>> {
    ensure!(
        bytes.starts_with(b"MSCF") && bytes.len() >= 36,
        crate::error::Error::malformed("invalid CAB signature/header")
    );
    ensure!(
        u32_at(bytes, 4)? == 0 && u32_at(bytes, 12)? == 0 && u32_at(bytes, 20)? == 0,
        crate::error::Error::malformed("invalid CAB reserved fields")
    );
    ensure!(
        bytes[24] == 3 && bytes[25] == 1,
        crate::error::Error::unsupported("unsupported CAB version")
    );
    let flags = u16_at(bytes, 30)?;
    ensure!(flags & !7 == 0, "unknown CAB flags");
    let cabinet_size = u32_at(bytes, 8)? as usize;
    let files_offset = u32_at(bytes, 16)? as usize;
    let folders = u16_at(bytes, 26)? as usize;
    let (header_end, payload_end, signed) = if flags & 4 != 0 {
        ensure!(
            u32_at(bytes, 36)? == 20 && u32_at(bytes, 40)? == 0x0010_0000,
            crate::error::Error::unsupported("unsupported CAB reserve/SIP layout")
        );
        let signature = u32_at(bytes, 44)? as usize;
        let signature_size = u32_at(bytes, 48)? as usize;
        ensure!(
            signature >= 60
                && signature == cabinet_size
                && signature_size > 0
                && signature.checked_add(signature_size) == Some(bytes.len()),
            crate::error::Error::malformed("invalid signed CAB signature range")
        );
        (60, signature, true)
    } else {
        ensure!(
            cabinet_size == bytes.len(),
            "CAB size differs from file extent"
        );
        (36, bytes.len(), false)
    };
    let mut folder_start = header_end;
    for flag in [1, 2] {
        if flags & flag != 0 {
            for _ in 0..2 {
                let remaining = range(
                    bytes,
                    folder_start,
                    payload_end
                        .checked_sub(folder_start)
                        .context("CAB name outside payload")?,
                )?;
                let length = remaining
                    .iter()
                    .position(|byte| *byte == 0)
                    .context("unterminated CAB volume name")?
                    + 1;
                folder_start += length;
            }
        }
    }
    let folder_end = folder_start
        .checked_add(folders * 8)
        .context("CAB folder range overflow")?;
    ensure!(
        folder_end == files_offset && folder_end <= payload_end,
        "CAB file table does not follow folder table"
    );
    for folder in 0..folders {
        let data = u32_at(bytes, folder_start + folder * 8)? as usize;
        ensure!(
            data >= files_offset && data <= payload_end,
            "CAB folder data outside payload"
        );
    }
    if signed {
        Ok(vec![
            range(bytes, 0, 4)?,
            range(bytes, 8, 26)?,
            range(bytes, 56, payload_end - 56)?,
        ])
    } else {
        Ok(vec![range(bytes, 0, 4)?, range(bytes, 8, bytes.len() - 8)?])
    }
}