blob: 1a79c831641f9fadf42bc123a78706fa42a9a313 [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 Wang1f0add02023-01-23 16:22:53 +000017use crate::descriptor::{Digest, HashDescriptors};
Alice Wangf2752862023-01-18 11:51:25 +000018use 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 Wang1f0add02023-01-23 16:22:53 +000024/// Verified data returned when the payload verification succeeds.
25#[derive(Debug)]
26pub struct VerifiedBootData {
27 /// DebugLevel of the VM.
28 pub debug_level: DebugLevel,
29 /// Kernel digest.
30 pub kernel_digest: Digest,
31 /// Initrd digest if initrd exists.
32 pub initrd_digest: Option<Digest>,
33}
34
Alice Wang5c1a7562023-01-13 17:19:57 +000035/// This enum corresponds to the `DebugLevel` in `VirtualMachineConfig`.
Alice Wang1f0add02023-01-23 16:22:53 +000036#[derive(Clone, Copy, Debug, PartialEq, Eq)]
Alice Wang5c1a7562023-01-13 17:19:57 +000037pub enum DebugLevel {
38 /// Not debuggable at all.
39 None,
40 /// Fully debuggable.
41 Full,
42}
43
Alice Wangd3f28ae2023-01-25 10:41:40 +000044fn verify_only_one_vbmeta_exists(
45 vbmeta_images: &[AvbVBMetaData],
46) -> Result<(), AvbSlotVerifyError> {
47 if vbmeta_images.len() == 1 {
48 Ok(())
49 } else {
50 Err(AvbSlotVerifyError::InvalidMetadata)
51 }
52}
53
Alice Wang9dfb2962023-01-18 10:01:34 +000054fn verify_vbmeta_is_from_kernel_partition(
55 vbmeta_image: &AvbVBMetaData,
56) -> Result<(), AvbSlotVerifyError> {
57 match (vbmeta_image.partition_name as *const c_char).try_into() {
58 Ok(PartitionName::Kernel) => Ok(()),
59 _ => Err(AvbSlotVerifyError::InvalidMetadata),
60 }
61}
62
Alice Wangf2752862023-01-18 11:51:25 +000063fn verify_vbmeta_has_only_one_hash_descriptor(
64 hash_descriptors: &HashDescriptors,
65) -> Result<(), AvbSlotVerifyError> {
66 if hash_descriptors.len() == 1 {
67 Ok(())
68 } else {
69 Err(AvbSlotVerifyError::InvalidMetadata)
70 }
71}
72
Alice Wang75d05632023-01-25 13:31:18 +000073fn verify_loaded_partition_has_expected_length(
74 loaded_partitions: &[AvbPartitionData],
75 partition_name: PartitionName,
76 expected_len: usize,
77) -> Result<(), AvbSlotVerifyError> {
78 if loaded_partitions.len() != 1 {
79 // Only one partition should be loaded in each verify result.
80 return Err(AvbSlotVerifyError::Io);
81 }
82 let loaded_partition = loaded_partitions[0];
83 if !PartitionName::try_from(loaded_partition.partition_name as *const c_char)
84 .map_or(false, |p| p == partition_name)
85 {
86 // Only the requested partition should be loaded.
87 return Err(AvbSlotVerifyError::Io);
88 }
89 if loaded_partition.data_size == expected_len {
90 Ok(())
91 } else {
92 Err(AvbSlotVerifyError::Verification)
93 }
94}
95
Alice Wangf3d96b12022-12-15 13:10:47 +000096/// Verifies the payload (signed kernel + initrd) against the trusted public key.
Alice Wang6b486f12023-01-06 13:12:16 +000097pub fn verify_payload(
98 kernel: &[u8],
99 initrd: Option<&[u8]>,
100 trusted_public_key: &[u8],
Alice Wang1f0add02023-01-23 16:22:53 +0000101) -> Result<VerifiedBootData, AvbSlotVerifyError> {
Alice Wang167ab3f2023-01-23 13:39:25 +0000102 let mut payload = Payload::new(kernel, initrd, trusted_public_key);
103 let mut ops = Ops::from(&mut payload);
104 let kernel_verify_result = ops.verify_partition(PartitionName::Kernel.as_cstr())?;
Alice Wang75d05632023-01-25 13:31:18 +0000105
Alice Wang86383df2023-01-11 10:03:56 +0000106 let vbmeta_images = kernel_verify_result.vbmeta_images()?;
Alice Wangd3f28ae2023-01-25 10:41:40 +0000107 verify_only_one_vbmeta_exists(vbmeta_images)?;
Alice Wang9dfb2962023-01-18 10:01:34 +0000108 let vbmeta_image = vbmeta_images[0];
109 verify_vbmeta_is_from_kernel_partition(&vbmeta_image)?;
Alice Wangf2752862023-01-18 11:51:25 +0000110 // SAFETY: It is safe because the `vbmeta_image` is collected from `AvbSlotVerifyData`,
111 // which is returned by `avb_slot_verify()` when the verification succeeds. It is
112 // guaranteed by libavb to be non-null and to point to a valid VBMeta structure.
113 let hash_descriptors = unsafe { HashDescriptors::from_vbmeta(vbmeta_image)? };
Alice Wang1f0add02023-01-23 16:22:53 +0000114 let kernel_descriptor = hash_descriptors.find(PartitionName::Kernel)?;
Alice Wangd3f28ae2023-01-25 10:41:40 +0000115
Alice Wang167ab3f2023-01-23 13:39:25 +0000116 if initrd.is_none() {
Alice Wangf2752862023-01-18 11:51:25 +0000117 verify_vbmeta_has_only_one_hash_descriptor(&hash_descriptors)?;
Alice Wang1f0add02023-01-23 16:22:53 +0000118 return Ok(VerifiedBootData {
119 debug_level: DebugLevel::None,
120 kernel_digest: kernel_descriptor.digest,
121 initrd_digest: None,
122 });
Alice Wang86383df2023-01-11 10:03:56 +0000123 }
Alice Wang5c1a7562023-01-13 17:19:57 +0000124
Alice Wang75d05632023-01-25 13:31:18 +0000125 let initrd = initrd.unwrap();
126 let (debug_level, initrd_verify_result, initrd_partition_name) =
127 if let Ok(result) = ops.verify_partition(PartitionName::InitrdNormal.as_cstr()) {
128 (DebugLevel::None, result, PartitionName::InitrdNormal)
129 } else if let Ok(result) = ops.verify_partition(PartitionName::InitrdDebug.as_cstr()) {
130 (DebugLevel::Full, result, PartitionName::InitrdDebug)
131 } else {
132 return Err(AvbSlotVerifyError::Verification);
133 };
134 let loaded_partitions = initrd_verify_result.loaded_partitions()?;
135 verify_loaded_partition_has_expected_length(
136 loaded_partitions,
137 initrd_partition_name,
138 initrd.len(),
139 )?;
Alice Wang1f0add02023-01-23 16:22:53 +0000140 let initrd_descriptor = hash_descriptors.find(initrd_partition_name)?;
141 Ok(VerifiedBootData {
142 debug_level,
143 kernel_digest: kernel_descriptor.digest,
144 initrd_digest: Some(initrd_descriptor.digest),
145 })
Alice Wang28cbcf12022-12-01 07:58:28 +0000146}