blob: 14b0e7e47853b8e7045892bf3c30b7257384dee7 [file] [log] [blame]
Alice Wang28cbcf12022-12-01 07:58:28 +00001// Copyright 2022, The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Alice Wangf3d96b12022-12-15 13:10:47 +000015//! This module handles the pvmfw payload verification.
Alice Wang28cbcf12022-12-01 07:58:28 +000016
Alice Wangf2752862023-01-18 11:51:25 +000017use crate::descriptor::HashDescriptors;
18use crate::error::AvbSlotVerifyError;
Alice Wang167ab3f2023-01-23 13:39:25 +000019use crate::ops::{Ops, Payload};
Alice Wang8077a862023-01-18 16:06:37 +000020use crate::partition::PartitionName;
Alice Wangf2752862023-01-18 11:51:25 +000021use avb_bindgen::{AvbPartitionData, AvbVBMetaData};
22use core::ffi::c_char;
Alice Wang28cbcf12022-12-01 07:58:28 +000023
Alice Wang5c1a7562023-01-13 17:19:57 +000024/// This enum corresponds to the `DebugLevel` in `VirtualMachineConfig`.
25#[derive(Clone, Debug, PartialEq, Eq)]
26pub enum DebugLevel {
27 /// Not debuggable at all.
28 None,
29 /// Fully debuggable.
30 Full,
31}
32
Alice Wangd3f28ae2023-01-25 10:41:40 +000033fn verify_only_one_vbmeta_exists(
34 vbmeta_images: &[AvbVBMetaData],
35) -> Result<(), AvbSlotVerifyError> {
36 if vbmeta_images.len() == 1 {
37 Ok(())
38 } else {
39 Err(AvbSlotVerifyError::InvalidMetadata)
40 }
41}
42
Alice Wang9dfb2962023-01-18 10:01:34 +000043fn verify_vbmeta_is_from_kernel_partition(
44 vbmeta_image: &AvbVBMetaData,
45) -> Result<(), AvbSlotVerifyError> {
46 match (vbmeta_image.partition_name as *const c_char).try_into() {
47 Ok(PartitionName::Kernel) => Ok(()),
48 _ => Err(AvbSlotVerifyError::InvalidMetadata),
49 }
50}
51
Alice Wangf2752862023-01-18 11:51:25 +000052fn verify_vbmeta_has_only_one_hash_descriptor(
53 hash_descriptors: &HashDescriptors,
54) -> Result<(), AvbSlotVerifyError> {
55 if hash_descriptors.len() == 1 {
56 Ok(())
57 } else {
58 Err(AvbSlotVerifyError::InvalidMetadata)
59 }
60}
61
Alice Wang75d05632023-01-25 13:31:18 +000062fn verify_loaded_partition_has_expected_length(
63 loaded_partitions: &[AvbPartitionData],
64 partition_name: PartitionName,
65 expected_len: usize,
66) -> Result<(), AvbSlotVerifyError> {
67 if loaded_partitions.len() != 1 {
68 // Only one partition should be loaded in each verify result.
69 return Err(AvbSlotVerifyError::Io);
70 }
71 let loaded_partition = loaded_partitions[0];
72 if !PartitionName::try_from(loaded_partition.partition_name as *const c_char)
73 .map_or(false, |p| p == partition_name)
74 {
75 // Only the requested partition should be loaded.
76 return Err(AvbSlotVerifyError::Io);
77 }
78 if loaded_partition.data_size == expected_len {
79 Ok(())
80 } else {
81 Err(AvbSlotVerifyError::Verification)
82 }
83}
84
Alice Wangf3d96b12022-12-15 13:10:47 +000085/// Verifies the payload (signed kernel + initrd) against the trusted public key.
Alice Wang6b486f12023-01-06 13:12:16 +000086pub fn verify_payload(
87 kernel: &[u8],
88 initrd: Option<&[u8]>,
89 trusted_public_key: &[u8],
Alice Wang5c1a7562023-01-13 17:19:57 +000090) -> Result<DebugLevel, AvbSlotVerifyError> {
Alice Wang167ab3f2023-01-23 13:39:25 +000091 let mut payload = Payload::new(kernel, initrd, trusted_public_key);
92 let mut ops = Ops::from(&mut payload);
93 let kernel_verify_result = ops.verify_partition(PartitionName::Kernel.as_cstr())?;
Alice Wang75d05632023-01-25 13:31:18 +000094
Alice Wang86383df2023-01-11 10:03:56 +000095 let vbmeta_images = kernel_verify_result.vbmeta_images()?;
Alice Wangd3f28ae2023-01-25 10:41:40 +000096 verify_only_one_vbmeta_exists(vbmeta_images)?;
Alice Wang9dfb2962023-01-18 10:01:34 +000097 let vbmeta_image = vbmeta_images[0];
98 verify_vbmeta_is_from_kernel_partition(&vbmeta_image)?;
Alice Wangf2752862023-01-18 11:51:25 +000099 // SAFETY: It is safe because the `vbmeta_image` is collected from `AvbSlotVerifyData`,
100 // which is returned by `avb_slot_verify()` when the verification succeeds. It is
101 // guaranteed by libavb to be non-null and to point to a valid VBMeta structure.
102 let hash_descriptors = unsafe { HashDescriptors::from_vbmeta(vbmeta_image)? };
103 // TODO(b/265897559): Pass the digest in kernel descriptor to DICE.
104 let _kernel_descriptor = hash_descriptors.find(PartitionName::Kernel)?;
Alice Wangd3f28ae2023-01-25 10:41:40 +0000105
Alice Wang167ab3f2023-01-23 13:39:25 +0000106 if initrd.is_none() {
Alice Wangf2752862023-01-18 11:51:25 +0000107 verify_vbmeta_has_only_one_hash_descriptor(&hash_descriptors)?;
Alice Wang5c1a7562023-01-13 17:19:57 +0000108 return Ok(DebugLevel::None);
Alice Wang86383df2023-01-11 10:03:56 +0000109 }
Alice Wang5c1a7562023-01-13 17:19:57 +0000110
Alice Wang75d05632023-01-25 13:31:18 +0000111 let initrd = initrd.unwrap();
112 let (debug_level, initrd_verify_result, initrd_partition_name) =
113 if let Ok(result) = ops.verify_partition(PartitionName::InitrdNormal.as_cstr()) {
114 (DebugLevel::None, result, PartitionName::InitrdNormal)
115 } else if let Ok(result) = ops.verify_partition(PartitionName::InitrdDebug.as_cstr()) {
116 (DebugLevel::Full, result, PartitionName::InitrdDebug)
117 } else {
118 return Err(AvbSlotVerifyError::Verification);
119 };
120 let loaded_partitions = initrd_verify_result.loaded_partitions()?;
121 verify_loaded_partition_has_expected_length(
122 loaded_partitions,
123 initrd_partition_name,
124 initrd.len(),
125 )?;
Alice Wang5c1a7562023-01-13 17:19:57 +0000126 Ok(debug_level)
Alice Wang28cbcf12022-12-01 07:58:28 +0000127}