authenticode/
lib.rs

1// Copyright 2023 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9//! Authenticode utilities.
10//!
11//! Reference:
12//! <https://docs.microsoft.com/en-us/windows/win32/debug/pe-format>
13//!
14//! # Features
15//!
16//! All features are disabled by default.
17//!
18//! * `object`: Enables a dependency on [`object`] and impls [`PeTrait`]
19//!   for [`PeFile`].
20//! * `std`: Turns off `no_std` and impls [`std::error::Error`] for the
21//!   error types.
22//!
23//! [`PeFile`]: https://docs.rs/object/latest/object/read/pe/struct.PeFile.html
24//! [`object`]: https://docs.rs/object/latest/object/
25//! [`std::error::Error`]: https://doc.rust-lang.org/std/error/trait.Error.html
26
27#![forbid(unsafe_code)]
28#![cfg_attr(docsrs, feature(doc_auto_cfg))]
29// Allow using `std` if the `std` feature is enabled, or when running
30// tests. Otherwise enable `no_std`.
31#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
32#![warn(clippy::arithmetic_side_effects)]
33#![warn(missing_docs)]
34
35extern crate alloc;
36
37mod authenticode_digest;
38mod pe;
39mod signature;
40mod win_cert;
41
42#[cfg(feature = "object")]
43mod pe_object;
44
45use core::convert::TryInto;
46
47pub use authenticode_digest::authenticode_digest;
48pub use pe::{PeOffsetError, PeOffsets, PeTrait};
49pub use signature::{
50    AuthenticodeSignature, AuthenticodeSignatureParseError, DigestInfo,
51    SpcAttributeTypeAndOptionalValue, SpcIndirectDataContent,
52    SPC_INDIRECT_DATA_OBJID,
53};
54pub use win_cert::{
55    AttributeCertificate, AttributeCertificateAuthenticodeError,
56    AttributeCertificateError, AttributeCertificateIterator,
57    WIN_CERT_REVISION_2_0, WIN_CERT_TYPE_PKCS_SIGNED_DATA,
58};
59
60/// Convert a `u32` to a `usize`, panicking if the value does not fit.
61///
62/// This can only panic on targets where `usize` is smaller than 32
63/// bits, which is not considered a supported use case by this library.
64fn usize_from_u32(val: u32) -> usize {
65    val.try_into().unwrap()
66}