interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / vendor / uefi / src / proto / shim / mod.rs

131 lines · 4250 bytes Raw Blame History
1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3//! Shim lock protocol.
4
5#![cfg(any(
6 target_arch = "x86",
7 target_arch = "x86_64",
8 target_arch = "arm",
9 target_arch = "aarch64"
10))]
11
12use crate::proto::unsafe_protocol;
13use crate::result::Error;
14use crate::{Result, Status, StatusExt};
15use core::ffi::c_void;
16use core::mem::MaybeUninit;
17
18// The `PE_COFF_LOADER_IMAGE_CONTEXT` type. None of our methods need to inspect
19// the fields of this struct, we just need to make sure it is the right size.
20#[repr(C)]
21struct Context {
22 _image_address: u64,
23 _image_size: u64,
24 _entry_point: u64,
25 _size_of_headers: usize,
26 _image_type: u16,
27 _number_of_sections: u16,
28 _section_alignment: u32,
29 _first_section: *const c_void,
30 _reloc_dir: *const c_void,
31 _sec_dir: *const c_void,
32 _number_of_rva_and_sizes: u64,
33 _pe_hdr: *const c_void,
34}
35
36const SHA1_DIGEST_SIZE: usize = 20;
37const SHA256_DIGEST_SIZE: usize = 32;
38
39/// Authenticode hashes of some UEFI application.
40#[derive(Debug)]
41pub struct Hashes {
42 /// SHA256 Authenticode Digest
43 pub sha256: [u8; SHA256_DIGEST_SIZE],
44 /// SHA1 Authenticode Digest
45 pub sha1: [u8; SHA1_DIGEST_SIZE],
46}
47
48// These macros set the correct calling convention for the Shim protocol methods.
49
50#[cfg(target_arch = "x86")]
51macro_rules! shim_function {
52 (fn $args:tt -> $return_type:ty) => (extern "cdecl" fn $args -> $return_type)
53}
54
55#[cfg(target_arch = "x86_64")]
56macro_rules! shim_function {
57 (fn $args:tt -> $return_type:ty) => (extern "sysv64" fn $args -> $return_type)
58}
59
60#[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
61macro_rules! shim_function {
62 (fn $args:tt -> $return_type:ty) => (extern "C" fn $args -> $return_type)
63}
64
65/// Shim Lock [`Protocol`].
66///
67/// This protocol is not part of the UEFI specification, but is
68/// installed by the [Shim bootloader](https://github.com/rhboot/shim)
69/// which is commonly used by Linux distributions to support UEFI
70/// Secure Boot. Shim is built with an embedded certificate that is
71/// used to validate another EFI application before running it. That
72/// application may itself be a bootloader that needs to validate
73/// another EFI application before running it, and the shim lock
74/// protocol exists to support that.
75///
76/// [`Protocol`]: uefi::proto::Protocol
77#[derive(Debug)]
78#[repr(C)]
79#[unsafe_protocol("605dab50-e046-4300-abb6-3dd810dd8b23")]
80pub struct ShimLock {
81 verify: shim_function! { fn(buffer: *const u8, size: u32) -> Status },
82 hash: shim_function! {
83 fn(
84 buffer: *const u8,
85 size: u32,
86 context: *mut Context,
87 sha256: *mut [u8; SHA256_DIGEST_SIZE],
88 sha1: *mut [u8; SHA1_DIGEST_SIZE]
89 ) -> Status
90 },
91 context: shim_function! { fn(buffer: *const u8, size: u32, context: *mut Context) -> Status },
92}
93
94impl ShimLock {
95 /// Verify that an EFI application is signed by the certificate
96 /// embedded in shim.
97 ///
98 /// The buffer's size must fit in a `u32`; if that condition is not
99 /// met then a `BAD_BUFFER_SIZE` error will be returned and the shim
100 /// lock protocol will not be called.
101 pub fn verify(&self, buffer: &[u8]) -> Result {
102 let size: u32 = buffer
103 .len()
104 .try_into()
105 .map_err(|_| Error::from(Status::BAD_BUFFER_SIZE))?;
106 (self.verify)(buffer.as_ptr(), size).to_result()
107 }
108 /// Compute the Authenticode Hash of the provided EFI application.
109 ///
110 /// The buffer's size must fit in a `u32`; if that condition is not
111 /// met then a `BAD_BUFFER_SIZE` error will be returned and the shim
112 /// lock protocol will not be called.
113 pub fn hash(&self, buffer: &[u8], hashes: &mut Hashes) -> Result {
114 let ptr: *const u8 = buffer.as_ptr();
115 let size: u32 = buffer
116 .len()
117 .try_into()
118 .map_err(|_| Error::from(Status::BAD_BUFFER_SIZE))?;
119
120 let mut context = MaybeUninit::<Context>::uninit();
121 (self.context)(ptr, size, context.as_mut_ptr()).to_result()?;
122 (self.hash)(
123 ptr,
124 size,
125 context.as_mut_ptr(),
126 &mut hashes.sha256,
127 &mut hashes.sha1,
128 )
129 .to_result()
130 }
131}