blob: 70eba5fd8deb8eeec3cdd1ed1ec1b841ae475e9d [file] [log] [blame]
Alice Wang2925b0a2023-01-19 10:44:24 +00001/*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Alice Wangeccdd392023-01-23 08:50:27 +000017//! Utility functions used by API tests.
Alice Wang2925b0a2023-01-19 10:44:24 +000018
Alice Wang1f0add02023-01-23 16:22:53 +000019use anyhow::{anyhow, Result};
Alice Wang2925b0a2023-01-19 10:44:24 +000020use avb_bindgen::{
21 avb_footer_validate_and_byteswap, avb_vbmeta_image_header_to_host_byte_order, AvbFooter,
22 AvbVBMetaImageHeader,
23};
Alice Wang1f0add02023-01-23 16:22:53 +000024use openssl::sha;
David Pursella7c727b2023-08-14 16:24:40 -070025use pvmfw_avb::{verify_payload, DebugLevel, Digest, PvmfwVerifyError, VerifiedBootData};
Alice Wang2925b0a2023-01-19 10:44:24 +000026use std::{
27 fs,
28 mem::{size_of, transmute, MaybeUninit},
29};
30
31const MICRODROID_KERNEL_IMG_PATH: &str = "microdroid_kernel";
32const INITRD_NORMAL_IMG_PATH: &str = "microdroid_initrd_normal.img";
33const INITRD_DEBUG_IMG_PATH: &str = "microdroid_initrd_debuggable.img";
34const PUBLIC_KEY_RSA4096_PATH: &str = "data/testkey_rsa4096_pub.bin";
35
36pub const PUBLIC_KEY_RSA2048_PATH: &str = "data/testkey_rsa2048_pub.bin";
37
Alice Wang1f0add02023-01-23 16:22:53 +000038pub fn assert_payload_verification_with_initrd_fails(
Alice Wang2925b0a2023-01-19 10:44:24 +000039 kernel: &[u8],
40 initrd: &[u8],
41 trusted_public_key: &[u8],
David Pursella7c727b2023-08-14 16:24:40 -070042 expected_error: PvmfwVerifyError,
Alice Wang2925b0a2023-01-19 10:44:24 +000043) -> Result<()> {
Alice Wang1f0add02023-01-23 16:22:53 +000044 assert_payload_verification_fails(kernel, Some(initrd), trusted_public_key, expected_error)
Alice Wang2925b0a2023-01-19 10:44:24 +000045}
46
Alice Wang1f0add02023-01-23 16:22:53 +000047pub fn assert_payload_verification_fails(
Alice Wang2925b0a2023-01-19 10:44:24 +000048 kernel: &[u8],
49 initrd: Option<&[u8]>,
50 trusted_public_key: &[u8],
David Pursella7c727b2023-08-14 16:24:40 -070051 expected_error: PvmfwVerifyError,
Alice Wang2925b0a2023-01-19 10:44:24 +000052) -> Result<()> {
Alice Wang1f0add02023-01-23 16:22:53 +000053 assert_eq!(expected_error, verify_payload(kernel, initrd, trusted_public_key).unwrap_err());
Alice Wang2925b0a2023-01-19 10:44:24 +000054 Ok(())
55}
56
57pub fn load_latest_signed_kernel() -> Result<Vec<u8>> {
58 Ok(fs::read(MICRODROID_KERNEL_IMG_PATH)?)
59}
60
61pub fn load_latest_initrd_normal() -> Result<Vec<u8>> {
62 Ok(fs::read(INITRD_NORMAL_IMG_PATH)?)
63}
64
65pub fn load_latest_initrd_debug() -> Result<Vec<u8>> {
66 Ok(fs::read(INITRD_DEBUG_IMG_PATH)?)
67}
68
69pub fn load_trusted_public_key() -> Result<Vec<u8>> {
70 Ok(fs::read(PUBLIC_KEY_RSA4096_PATH)?)
71}
72
73pub fn extract_avb_footer(kernel: &[u8]) -> Result<AvbFooter> {
74 let footer_start = kernel.len() - size_of::<AvbFooter>();
75 // SAFETY: The slice is the same size as the struct which only contains simple data types.
76 let mut footer = unsafe {
77 transmute::<[u8; size_of::<AvbFooter>()], AvbFooter>(kernel[footer_start..].try_into()?)
78 };
79 // SAFETY: The function updates the struct in-place.
80 unsafe {
81 avb_footer_validate_and_byteswap(&footer, &mut footer);
82 }
83 Ok(footer)
84}
85
86pub fn extract_vbmeta_header(kernel: &[u8], footer: &AvbFooter) -> Result<AvbVBMetaImageHeader> {
87 let vbmeta_offset: usize = footer.vbmeta_offset.try_into()?;
88 let vbmeta_size: usize = footer.vbmeta_size.try_into()?;
89 let vbmeta_src = &kernel[vbmeta_offset..(vbmeta_offset + vbmeta_size)];
90 // SAFETY: The latest kernel has a valid VBMeta header at the position specified in footer.
91 let vbmeta_header = unsafe {
92 let mut header = MaybeUninit::uninit();
93 let src = vbmeta_src.as_ptr() as *const _ as *const AvbVBMetaImageHeader;
94 avb_vbmeta_image_header_to_host_byte_order(src, header.as_mut_ptr());
95 header.assume_init()
96 };
97 Ok(vbmeta_header)
98}
Alice Wang1f0add02023-01-23 16:22:53 +000099
100pub fn assert_latest_payload_verification_passes(
101 initrd: &[u8],
102 initrd_salt: &[u8],
103 expected_debug_level: DebugLevel,
104) -> Result<()> {
Pierre-Clément Tosif58f3a32023-02-02 16:24:23 +0000105 let public_key = load_trusted_public_key()?;
Alice Wang1f0add02023-01-23 16:22:53 +0000106 let kernel = load_latest_signed_kernel()?;
Pierre-Clément Tosif58f3a32023-02-02 16:24:23 +0000107 let verified_boot_data = verify_payload(&kernel, Some(initrd), &public_key)
Alice Wang1f0add02023-01-23 16:22:53 +0000108 .map_err(|e| anyhow!("Verification failed. Error: {}", e))?;
109
Alice Wang1f0add02023-01-23 16:22:53 +0000110 let footer = extract_avb_footer(&kernel)?;
Pierre-Clément Tosi81ca0802023-02-14 10:41:38 +0000111 let kernel_digest =
112 hash(&[&hash(&[b"bootloader"]), &kernel[..usize::try_from(footer.original_image_size)?]]);
113 let initrd_digest = Some(hash(&[&hash(&[initrd_salt]), initrd]));
Pierre-Clément Tosif58f3a32023-02-02 16:24:23 +0000114 let expected_boot_data = VerifiedBootData {
115 debug_level: expected_debug_level,
116 kernel_digest,
117 initrd_digest,
118 public_key: &public_key,
Alice Wangab0d0202023-05-17 08:07:41 +0000119 capabilities: vec![],
Shikha Panwara26f16a2023-09-27 09:39:00 +0000120 rollback_index: if cfg!(llpvm_changes) { 1 } else { 0 },
Pierre-Clément Tosif58f3a32023-02-02 16:24:23 +0000121 };
Pierre-Clément Tosi81ca0802023-02-14 10:41:38 +0000122 assert_eq!(expected_boot_data, verified_boot_data);
123
Alice Wang1f0add02023-01-23 16:22:53 +0000124 Ok(())
125}
126
127pub fn hash(inputs: &[&[u8]]) -> Digest {
128 let mut digester = sha::Sha256::new();
129 inputs.iter().for_each(|input| digester.update(input));
130 digester.finish()
131}