Alan Stokes | 16fb855 | 2022-02-10 15:07:27 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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::{bail, Context, Result}; |
| 18 | use std::io::Write; |
| 19 | use std::process::{Command, Stdio}; |
| 20 | |
| 21 | const COMPOS_KEY_HELPER_PATH: &str = "/apex/com.android.compos/bin/compos_key_helper"; |
| 22 | |
| 23 | pub fn get_public_key() -> Result<Vec<u8>> { |
Alan Stokes | 5430eca | 2022-03-21 14:02:09 +0000 | [diff] [blame] | 24 | get_data_from_helper("public_key") |
| 25 | } |
| 26 | |
| 27 | pub fn get_attestation_chain() -> Result<Vec<u8>> { |
| 28 | get_data_from_helper("bcc") |
| 29 | } |
| 30 | |
| 31 | fn get_data_from_helper(command: &str) -> Result<Vec<u8>> { |
Alan Stokes | 16fb855 | 2022-02-10 15:07:27 +0000 | [diff] [blame] | 32 | let child = Command::new(COMPOS_KEY_HELPER_PATH) |
Alan Stokes | 5430eca | 2022-03-21 14:02:09 +0000 | [diff] [blame] | 33 | .arg(command) |
Alan Stokes | 16fb855 | 2022-02-10 15:07:27 +0000 | [diff] [blame] | 34 | .stdin(Stdio::null()) |
| 35 | .stdout(Stdio::piped()) |
| 36 | .stderr(Stdio::piped()) |
| 37 | .spawn()?; |
| 38 | let result = child.wait_with_output()?; |
| 39 | if !result.status.success() { |
| 40 | bail!("Helper failed: {:?}", result); |
| 41 | } |
| 42 | Ok(result.stdout) |
| 43 | } |
| 44 | |
| 45 | pub fn sign(data: &[u8]) -> Result<Vec<u8>> { |
| 46 | let mut child = Command::new(COMPOS_KEY_HELPER_PATH) |
| 47 | .arg("sign") |
| 48 | .stdin(Stdio::piped()) |
| 49 | .stdout(Stdio::piped()) |
| 50 | .stderr(Stdio::piped()) |
| 51 | .spawn()?; |
| 52 | |
| 53 | // No output is written until the entire input is consumed, so this shouldn't deadlock. |
| 54 | let result = |
| 55 | child.stdin.take().unwrap().write_all(data).context("Failed to write data to be signed"); |
| 56 | if result.is_ok() { |
| 57 | let result = child.wait_with_output()?; |
| 58 | if !result.status.success() { |
| 59 | bail!("Helper failed: {}", result.status); |
| 60 | } |
| 61 | return Ok(result.stdout); |
| 62 | } |
| 63 | |
| 64 | // The child may have exited already, but if it hasn't then we need to make sure it does. |
| 65 | let _ignored = child.kill(); |
| 66 | |
| 67 | let result = result.with_context(|| match child.wait() { |
| 68 | Ok(exit_status) => format!("Child exited: {}", exit_status), |
| 69 | Err(wait_err) => format!("Wait for child failed: {:?}", wait_err), |
| 70 | }); |
| 71 | Err(result.unwrap_err()) |
| 72 | } |