Alan Stokes | 183d7d3 | 2021-12-08 16:10:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 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 | //! Support for generating and signing an info file listing names and digests of generated |
| 18 | //! artifacts. |
| 19 | |
Alan Stokes | 16fb855 | 2022-02-10 15:07:27 +0000 | [diff] [blame] | 20 | use crate::compos_key; |
Alan Stokes | 183d7d3 | 2021-12-08 16:10:45 +0000 | [diff] [blame] | 21 | use crate::fsverity; |
| 22 | use anyhow::{anyhow, Context, Result}; |
| 23 | use odsign_proto::odsign_info::OdsignInfo; |
| 24 | use protobuf::Message; |
| 25 | use std::fs::File; |
| 26 | use std::io::Write; |
Victor Hsieh | 4333d52 | 2022-08-26 11:27:08 -0700 | [diff] [blame] | 27 | use std::os::unix::io::AsFd; |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 28 | use std::path::Path; |
Alan Stokes | 183d7d3 | 2021-12-08 16:10:45 +0000 | [diff] [blame] | 29 | |
| 30 | const TARGET_DIRECTORY: &str = "/data/misc/apexdata/com.android.art/dalvik-cache"; |
| 31 | const SIGNATURE_EXTENSION: &str = ".signature"; |
| 32 | |
| 33 | /// Accumulates and then signs information about generated artifacts. |
| 34 | pub struct ArtifactSigner<'a> { |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 35 | base_directory: &'a Path, |
Alan Stokes | 183d7d3 | 2021-12-08 16:10:45 +0000 | [diff] [blame] | 36 | file_digests: Vec<(String, String)>, // (File name, digest in hex) |
| 37 | } |
| 38 | |
| 39 | impl<'a> ArtifactSigner<'a> { |
| 40 | /// base_directory specifies the directory under which the artifacts are currently located; |
| 41 | /// they will eventually be moved under TARGET_DIRECTORY once they are verified and activated. |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 42 | pub fn new(base_directory: &'a Path) -> Self { |
| 43 | Self { base_directory, file_digests: Vec::new() } |
Alan Stokes | 183d7d3 | 2021-12-08 16:10:45 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 46 | pub fn add_artifact(&mut self, path: &Path) -> Result<()> { |
Alan Stokes | 183d7d3 | 2021-12-08 16:10:45 +0000 | [diff] [blame] | 47 | // The path we store is where the file will be when it is verified, not where it is now. |
| 48 | let suffix = path |
Chris Wailes | 9b866f0 | 2022-11-16 15:17:16 -0800 | [diff] [blame^] | 49 | .strip_prefix(self.base_directory) |
Alan Stokes | 183d7d3 | 2021-12-08 16:10:45 +0000 | [diff] [blame] | 50 | .context("Artifacts must be under base directory")?; |
| 51 | let target_path = Path::new(TARGET_DIRECTORY).join(suffix); |
| 52 | let target_path = target_path.to_str().ok_or_else(|| anyhow!("Invalid path"))?; |
| 53 | |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 54 | let file = File::open(path).with_context(|| format!("Opening {}", path.display()))?; |
Victor Hsieh | 4333d52 | 2022-08-26 11:27:08 -0700 | [diff] [blame] | 55 | let digest = fsverity::measure(file.as_fd())?; |
Alan Stokes | 183d7d3 | 2021-12-08 16:10:45 +0000 | [diff] [blame] | 56 | let digest = to_hex_string(&digest); |
| 57 | |
| 58 | self.file_digests.push((target_path.to_owned(), digest)); |
| 59 | Ok(()) |
| 60 | } |
| 61 | |
| 62 | /// Consume this ArtifactSigner and write details of all its artifacts to the given path, |
| 63 | /// with accompanying sigature file. |
Alan Stokes | 16fb855 | 2022-02-10 15:07:27 +0000 | [diff] [blame] | 64 | pub fn write_info_and_signature(self, info_path: &Path) -> Result<()> { |
Alan Stokes | 183d7d3 | 2021-12-08 16:10:45 +0000 | [diff] [blame] | 65 | let mut info = OdsignInfo::new(); |
| 66 | info.mut_file_hashes().extend(self.file_digests.into_iter()); |
| 67 | let bytes = info.write_to_bytes()?; |
| 68 | |
Alan Stokes | 16fb855 | 2022-02-10 15:07:27 +0000 | [diff] [blame] | 69 | let signature = compos_key::sign(&bytes)?; |
Alan Stokes | 183d7d3 | 2021-12-08 16:10:45 +0000 | [diff] [blame] | 70 | |
Alan Stokes | 35bac3c | 2021-12-16 14:37:24 +0000 | [diff] [blame] | 71 | let mut file = |
| 72 | File::create(info_path).with_context(|| format!("Creating {}", info_path.display()))?; |
Alan Stokes | 183d7d3 | 2021-12-08 16:10:45 +0000 | [diff] [blame] | 73 | file.write_all(&bytes)?; |
| 74 | |
| 75 | let mut signature_name = info_path.file_name().unwrap().to_owned(); |
| 76 | signature_name.push(SIGNATURE_EXTENSION); |
| 77 | let signature_path = info_path.with_file_name(&signature_name); |
Alan Stokes | 35bac3c | 2021-12-16 14:37:24 +0000 | [diff] [blame] | 78 | let mut signature_file = File::create(&signature_path) |
| 79 | .with_context(|| format!("Creating {}", signature_path.display()))?; |
Alan Stokes | 183d7d3 | 2021-12-08 16:10:45 +0000 | [diff] [blame] | 80 | signature_file.write_all(&signature)?; |
| 81 | |
| 82 | Ok(()) |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | fn to_hex_string(buf: &[u8]) -> String { |
| 87 | buf.iter().map(|b| format!("{:02x}", b)).collect() |
| 88 | } |