Alice Wang | bf7fadd | 2023-01-13 12:18:24 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2022 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 | use anyhow::Result; |
| 18 | use avb_bindgen::AvbFooter; |
| 19 | use pvmfw_avb::{verify_payload, AvbSlotVerifyError}; |
| 20 | use std::{fs, mem::size_of}; |
| 21 | |
| 22 | const MICRODROID_KERNEL_IMG_PATH: &str = "microdroid_kernel"; |
| 23 | const INITRD_NORMAL_IMG_PATH: &str = "microdroid_initrd_normal.img"; |
| 24 | const TEST_IMG_WITH_ONE_HASHDESC_PATH: &str = "test_image_with_one_hashdesc.img"; |
| 25 | const UNSIGNED_TEST_IMG_PATH: &str = "unsigned_test.img"; |
| 26 | |
| 27 | const PUBLIC_KEY_RSA2048_PATH: &str = "data/testkey_rsa2048_pub.bin"; |
| 28 | const PUBLIC_KEY_RSA4096_PATH: &str = "data/testkey_rsa4096_pub.bin"; |
| 29 | const RANDOM_FOOTER_POS: usize = 30; |
| 30 | |
| 31 | /// This test uses the Microdroid payload compiled on the fly to check that |
| 32 | /// the latest payload can be verified successfully. |
| 33 | #[test] |
| 34 | fn latest_valid_payload_passes_verification() -> Result<()> { |
| 35 | let kernel = load_latest_signed_kernel()?; |
| 36 | let initrd_normal = fs::read(INITRD_NORMAL_IMG_PATH)?; |
| 37 | let public_key = fs::read(PUBLIC_KEY_RSA4096_PATH)?; |
| 38 | |
| 39 | assert_eq!(Ok(()), verify_payload(&kernel, Some(&initrd_normal[..]), &public_key)); |
| 40 | Ok(()) |
| 41 | } |
| 42 | |
| 43 | #[test] |
| 44 | fn payload_expecting_no_initrd_passes_verification_with_no_initrd() -> Result<()> { |
| 45 | let kernel = fs::read(TEST_IMG_WITH_ONE_HASHDESC_PATH)?; |
| 46 | let public_key = fs::read(PUBLIC_KEY_RSA4096_PATH)?; |
| 47 | |
| 48 | assert_eq!(Ok(()), verify_payload(&kernel, None, &public_key)); |
| 49 | Ok(()) |
| 50 | } |
| 51 | |
| 52 | // TODO(b/256148034): Test that kernel with two hashdesc and no initrd fails verification. |
| 53 | // e.g. payload_expecting_initrd_fails_verification_with_no_initrd |
| 54 | |
| 55 | #[test] |
| 56 | fn payload_with_empty_public_key_fails_verification() -> Result<()> { |
| 57 | assert_payload_verification_fails( |
| 58 | &load_latest_signed_kernel()?, |
| 59 | &load_latest_initrd_normal()?, |
| 60 | /*trusted_public_key=*/ &[0u8; 0], |
| 61 | AvbSlotVerifyError::PublicKeyRejected, |
| 62 | ) |
| 63 | } |
| 64 | |
| 65 | #[test] |
| 66 | fn payload_with_an_invalid_public_key_fails_verification() -> Result<()> { |
| 67 | assert_payload_verification_fails( |
| 68 | &load_latest_signed_kernel()?, |
| 69 | &load_latest_initrd_normal()?, |
| 70 | /*trusted_public_key=*/ &[0u8; 512], |
| 71 | AvbSlotVerifyError::PublicKeyRejected, |
| 72 | ) |
| 73 | } |
| 74 | |
| 75 | #[test] |
| 76 | fn payload_with_a_different_valid_public_key_fails_verification() -> Result<()> { |
| 77 | assert_payload_verification_fails( |
| 78 | &load_latest_signed_kernel()?, |
| 79 | &load_latest_initrd_normal()?, |
| 80 | &fs::read(PUBLIC_KEY_RSA2048_PATH)?, |
| 81 | AvbSlotVerifyError::PublicKeyRejected, |
| 82 | ) |
| 83 | } |
| 84 | |
| 85 | #[test] |
| 86 | fn unsigned_kernel_fails_verification() -> Result<()> { |
| 87 | assert_payload_verification_fails( |
| 88 | &fs::read(UNSIGNED_TEST_IMG_PATH)?, |
| 89 | &load_latest_initrd_normal()?, |
| 90 | &fs::read(PUBLIC_KEY_RSA4096_PATH)?, |
| 91 | AvbSlotVerifyError::Io, |
| 92 | ) |
| 93 | } |
| 94 | |
| 95 | #[test] |
| 96 | fn tampered_kernel_fails_verification() -> Result<()> { |
| 97 | let mut kernel = load_latest_signed_kernel()?; |
| 98 | kernel[1] = !kernel[1]; // Flip the bits |
| 99 | |
| 100 | assert_payload_verification_fails( |
| 101 | &kernel, |
| 102 | &load_latest_initrd_normal()?, |
| 103 | &fs::read(PUBLIC_KEY_RSA4096_PATH)?, |
| 104 | AvbSlotVerifyError::Verification, |
| 105 | ) |
| 106 | } |
| 107 | |
| 108 | #[test] |
| 109 | fn tampered_kernel_footer_fails_verification() -> Result<()> { |
| 110 | let mut kernel = load_latest_signed_kernel()?; |
| 111 | let avb_footer_index = kernel.len() - size_of::<AvbFooter>() + RANDOM_FOOTER_POS; |
| 112 | kernel[avb_footer_index] = !kernel[avb_footer_index]; |
| 113 | |
| 114 | assert_payload_verification_fails( |
| 115 | &kernel, |
| 116 | &load_latest_initrd_normal()?, |
| 117 | &fs::read(PUBLIC_KEY_RSA4096_PATH)?, |
| 118 | AvbSlotVerifyError::InvalidMetadata, |
| 119 | ) |
| 120 | } |
| 121 | |
| 122 | fn assert_payload_verification_fails( |
| 123 | kernel: &[u8], |
| 124 | initrd: &[u8], |
| 125 | trusted_public_key: &[u8], |
| 126 | expected_error: AvbSlotVerifyError, |
| 127 | ) -> Result<()> { |
| 128 | assert_eq!(Err(expected_error), verify_payload(kernel, Some(initrd), trusted_public_key)); |
| 129 | Ok(()) |
| 130 | } |
| 131 | |
| 132 | fn load_latest_signed_kernel() -> Result<Vec<u8>> { |
| 133 | Ok(fs::read(MICRODROID_KERNEL_IMG_PATH)?) |
| 134 | } |
| 135 | |
| 136 | fn load_latest_initrd_normal() -> Result<Vec<u8>> { |
| 137 | Ok(fs::read(INITRD_NORMAL_IMG_PATH)?) |
| 138 | } |