Alice Wang | 9c40eca | 2023-02-03 13:10:24 +0000 | [diff] [blame^] | 1 | // Copyright 2023, 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 | |
| 15 | //! This module mirrors the content in open-dice/include/dice/android/bcc.h |
| 16 | |
| 17 | use crate::error::{check_result, Result}; |
| 18 | use open_dice_bcc_bindgen::{ |
| 19 | BccConfigValues, BccFormatConfigDescriptor, BCC_INPUT_COMPONENT_NAME, |
| 20 | BCC_INPUT_COMPONENT_VERSION, BCC_INPUT_RESETTABLE, |
| 21 | }; |
| 22 | use std::{ffi::CStr, ptr}; |
| 23 | |
| 24 | /// Formats a configuration descriptor following the BCC's specification. |
| 25 | /// See https://cs.android.com/android/platform/superproject/+/master:hardware/interfaces/security/rkp/aidl/android/hardware/security/keymint/ProtectedData.aidl |
| 26 | pub fn bcc_format_config_descriptor( |
| 27 | name: Option<&CStr>, |
| 28 | version: Option<u64>, |
| 29 | resettable: bool, |
| 30 | buffer: &mut [u8], |
| 31 | ) -> Result<usize> { |
| 32 | let mut inputs = 0; |
| 33 | if name.is_some() { |
| 34 | inputs |= BCC_INPUT_COMPONENT_NAME; |
| 35 | } |
| 36 | if version.is_some() { |
| 37 | inputs |= BCC_INPUT_COMPONENT_VERSION; |
| 38 | } |
| 39 | if resettable { |
| 40 | inputs |= BCC_INPUT_RESETTABLE; |
| 41 | } |
| 42 | |
| 43 | let values = BccConfigValues { |
| 44 | inputs, |
| 45 | component_name: name.map_or(ptr::null(), |p| p.as_ptr()), |
| 46 | component_version: version.unwrap_or(0), |
| 47 | }; |
| 48 | |
| 49 | let mut buffer_size = 0; |
| 50 | // SAFETY: The function writes to the buffer, within the given bounds, and only reads the |
| 51 | // input values. It writes its result to buffer_size. |
| 52 | check_result(unsafe { |
| 53 | BccFormatConfigDescriptor(&values, buffer.len(), buffer.as_mut_ptr(), &mut buffer_size) |
| 54 | })?; |
| 55 | Ok(buffer_size) |
| 56 | } |