Alice Wang | 2925b0a | 2023-01-19 10:44:24 +0000 | [diff] [blame^] | 1 | /* |
| 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 | |
| 17 | //! Utility methods used by API tests. |
| 18 | |
| 19 | use anyhow::Result; |
| 20 | use avb_bindgen::{ |
| 21 | avb_footer_validate_and_byteswap, avb_vbmeta_image_header_to_host_byte_order, AvbFooter, |
| 22 | AvbVBMetaImageHeader, |
| 23 | }; |
| 24 | use pvmfw_avb::{verify_payload, AvbSlotVerifyError}; |
| 25 | use std::{ |
| 26 | fs, |
| 27 | mem::{size_of, transmute, MaybeUninit}, |
| 28 | }; |
| 29 | |
| 30 | const MICRODROID_KERNEL_IMG_PATH: &str = "microdroid_kernel"; |
| 31 | const INITRD_NORMAL_IMG_PATH: &str = "microdroid_initrd_normal.img"; |
| 32 | const INITRD_DEBUG_IMG_PATH: &str = "microdroid_initrd_debuggable.img"; |
| 33 | const PUBLIC_KEY_RSA4096_PATH: &str = "data/testkey_rsa4096_pub.bin"; |
| 34 | |
| 35 | pub const PUBLIC_KEY_RSA2048_PATH: &str = "data/testkey_rsa2048_pub.bin"; |
| 36 | |
| 37 | pub fn assert_payload_verification_with_initrd_eq( |
| 38 | kernel: &[u8], |
| 39 | initrd: &[u8], |
| 40 | trusted_public_key: &[u8], |
| 41 | expected_result: Result<(), AvbSlotVerifyError>, |
| 42 | ) -> Result<()> { |
| 43 | assert_payload_verification_eq(kernel, Some(initrd), trusted_public_key, expected_result) |
| 44 | } |
| 45 | |
| 46 | pub fn assert_payload_verification_eq( |
| 47 | kernel: &[u8], |
| 48 | initrd: Option<&[u8]>, |
| 49 | trusted_public_key: &[u8], |
| 50 | expected_result: Result<(), AvbSlotVerifyError>, |
| 51 | ) -> Result<()> { |
| 52 | assert_eq!(expected_result, verify_payload(kernel, initrd, trusted_public_key)); |
| 53 | Ok(()) |
| 54 | } |
| 55 | |
| 56 | pub fn load_latest_signed_kernel() -> Result<Vec<u8>> { |
| 57 | Ok(fs::read(MICRODROID_KERNEL_IMG_PATH)?) |
| 58 | } |
| 59 | |
| 60 | pub fn load_latest_initrd_normal() -> Result<Vec<u8>> { |
| 61 | Ok(fs::read(INITRD_NORMAL_IMG_PATH)?) |
| 62 | } |
| 63 | |
| 64 | pub fn load_latest_initrd_debug() -> Result<Vec<u8>> { |
| 65 | Ok(fs::read(INITRD_DEBUG_IMG_PATH)?) |
| 66 | } |
| 67 | |
| 68 | pub fn load_trusted_public_key() -> Result<Vec<u8>> { |
| 69 | Ok(fs::read(PUBLIC_KEY_RSA4096_PATH)?) |
| 70 | } |
| 71 | |
| 72 | pub fn extract_avb_footer(kernel: &[u8]) -> Result<AvbFooter> { |
| 73 | let footer_start = kernel.len() - size_of::<AvbFooter>(); |
| 74 | // SAFETY: The slice is the same size as the struct which only contains simple data types. |
| 75 | let mut footer = unsafe { |
| 76 | transmute::<[u8; size_of::<AvbFooter>()], AvbFooter>(kernel[footer_start..].try_into()?) |
| 77 | }; |
| 78 | // SAFETY: The function updates the struct in-place. |
| 79 | unsafe { |
| 80 | avb_footer_validate_and_byteswap(&footer, &mut footer); |
| 81 | } |
| 82 | Ok(footer) |
| 83 | } |
| 84 | |
| 85 | pub fn extract_vbmeta_header(kernel: &[u8], footer: &AvbFooter) -> Result<AvbVBMetaImageHeader> { |
| 86 | let vbmeta_offset: usize = footer.vbmeta_offset.try_into()?; |
| 87 | let vbmeta_size: usize = footer.vbmeta_size.try_into()?; |
| 88 | let vbmeta_src = &kernel[vbmeta_offset..(vbmeta_offset + vbmeta_size)]; |
| 89 | // SAFETY: The latest kernel has a valid VBMeta header at the position specified in footer. |
| 90 | let vbmeta_header = unsafe { |
| 91 | let mut header = MaybeUninit::uninit(); |
| 92 | let src = vbmeta_src.as_ptr() as *const _ as *const AvbVBMetaImageHeader; |
| 93 | avb_vbmeta_image_header_to_host_byte_order(src, header.as_mut_ptr()); |
| 94 | header.assume_init() |
| 95 | }; |
| 96 | Ok(vbmeta_header) |
| 97 | } |