Janis Danisevskis | a75e208 | 2020-10-07 16:44:26 -0700 | [diff] [blame] | 1 | // Copyright 2020, 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 implements utility functions used by the Keystore 2.0 service |
| 16 | //! implementation. |
| 17 | |
Janis Danisevskis | a75e208 | 2020-10-07 16:44:26 -0700 | [diff] [blame] | 18 | use crate::permission; |
| 19 | use crate::permission::{KeyPerm, KeyPermSet, KeystorePerm}; |
Janis Danisevskis | 04b0283 | 2020-10-26 09:21:40 -0700 | [diff] [blame] | 20 | use crate::{error::Error, key_parameter::KeyParameterValue}; |
Shawn Willden | 708744a | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 21 | use android_hardware_security_keymint::aidl::android::hardware::security::keymint::{ |
Janis Danisevskis | a53c9cf | 2020-10-26 11:52:33 -0700 | [diff] [blame] | 22 | KeyCharacteristics::KeyCharacteristics, SecurityLevel::SecurityLevel, |
Janis Danisevskis | a75e208 | 2020-10-07 16:44:26 -0700 | [diff] [blame] | 23 | }; |
| 24 | use android_system_keystore2::aidl::android::system::keystore2::{ |
Janis Danisevskis | a53c9cf | 2020-10-26 11:52:33 -0700 | [diff] [blame] | 25 | Authorization::Authorization, KeyDescriptor::KeyDescriptor, |
Janis Danisevskis | a75e208 | 2020-10-07 16:44:26 -0700 | [diff] [blame] | 26 | }; |
| 27 | use anyhow::{anyhow, Context}; |
| 28 | use binder::{FromIBinder, SpIBinder, ThreadState}; |
| 29 | use std::sync::Mutex; |
| 30 | |
| 31 | /// This function uses its namesake in the permission module and in |
| 32 | /// combination with with_calling_sid from the binder crate to check |
| 33 | /// if the caller has the given keystore permission. |
| 34 | pub fn check_keystore_permission(perm: KeystorePerm) -> anyhow::Result<()> { |
| 35 | ThreadState::with_calling_sid(|calling_sid| { |
| 36 | permission::check_keystore_permission( |
| 37 | &calling_sid.ok_or_else(Error::sys).context( |
| 38 | "In check_keystore_permission: Cannot check permission without calling_sid.", |
| 39 | )?, |
| 40 | perm, |
| 41 | ) |
| 42 | }) |
| 43 | } |
| 44 | |
| 45 | /// This function uses its namesake in the permission module and in |
| 46 | /// combination with with_calling_sid from the binder crate to check |
| 47 | /// if the caller has the given grant permission. |
| 48 | pub fn check_grant_permission(access_vec: KeyPermSet, key: &KeyDescriptor) -> anyhow::Result<()> { |
| 49 | ThreadState::with_calling_sid(|calling_sid| { |
| 50 | permission::check_grant_permission( |
| 51 | &calling_sid.ok_or_else(Error::sys).context( |
| 52 | "In check_grant_permission: Cannot check permission without calling_sid.", |
| 53 | )?, |
| 54 | access_vec, |
| 55 | key, |
| 56 | ) |
| 57 | }) |
| 58 | } |
| 59 | |
| 60 | /// This function uses its namesake in the permission module and in |
| 61 | /// combination with with_calling_sid from the binder crate to check |
| 62 | /// if the caller has the given key permission. |
| 63 | pub fn check_key_permission( |
| 64 | perm: KeyPerm, |
| 65 | key: &KeyDescriptor, |
| 66 | access_vector: &Option<KeyPermSet>, |
| 67 | ) -> anyhow::Result<()> { |
| 68 | ThreadState::with_calling_sid(|calling_sid| { |
| 69 | permission::check_key_permission( |
| 70 | &calling_sid |
| 71 | .ok_or_else(Error::sys) |
| 72 | .context("In check_key_permission: Cannot check permission without calling_sid.")?, |
| 73 | perm, |
| 74 | key, |
| 75 | access_vector, |
| 76 | ) |
| 77 | }) |
| 78 | } |
| 79 | |
Janis Danisevskis | a75e208 | 2020-10-07 16:44:26 -0700 | [diff] [blame] | 80 | /// Thread safe wrapper around SpIBinder. It is safe to have SpIBinder smart pointers to the |
| 81 | /// same object in multiple threads, but cloning a SpIBinder is not thread safe. |
| 82 | /// Keystore frequently hands out binder tokens to the security level interface. If this |
| 83 | /// is to happen from a multi threaded thread pool, the SpIBinder needs to be protected by a |
| 84 | /// Mutex. |
| 85 | #[derive(Debug)] |
| 86 | pub struct Asp(Mutex<SpIBinder>); |
| 87 | |
| 88 | impl Asp { |
| 89 | /// Creates a new instance owning a SpIBinder wrapped in a Mutex. |
| 90 | pub fn new(i: SpIBinder) -> Self { |
| 91 | Self(Mutex::new(i)) |
| 92 | } |
| 93 | |
| 94 | /// Clones the owned SpIBinder and attempts to convert it into the requested interface. |
| 95 | pub fn get_interface<T: FromIBinder + ?Sized>(&self) -> anyhow::Result<Box<T>> { |
| 96 | // We can use unwrap here because we never panic when locked, so the mutex |
| 97 | // can never be poisoned. |
| 98 | let lock = self.0.lock().unwrap(); |
| 99 | (*lock) |
| 100 | .clone() |
| 101 | .into_interface() |
| 102 | .map_err(|e| anyhow!(format!("get_interface failed with error code {:?}", e))) |
| 103 | } |
| 104 | } |
Janis Danisevskis | 04b0283 | 2020-10-26 09:21:40 -0700 | [diff] [blame] | 105 | |
| 106 | /// Converts a set of key characteristics as returned from KeyMint into the internal |
| 107 | /// representation of the keystore service. |
| 108 | /// The parameter `hw_security_level` indicates which security level shall be used for |
| 109 | /// parameters found in the hardware enforced parameter list. |
| 110 | pub fn key_characteristics_to_internal( |
| 111 | key_characteristics: KeyCharacteristics, |
| 112 | hw_security_level: SecurityLevel, |
| 113 | ) -> Vec<crate::key_parameter::KeyParameter> { |
| 114 | key_characteristics |
| 115 | .hardwareEnforced |
| 116 | .into_iter() |
| 117 | .map(|aidl_kp| { |
| 118 | crate::key_parameter::KeyParameter::new( |
| 119 | KeyParameterValue::convert_from_wire(aidl_kp), |
| 120 | hw_security_level, |
| 121 | ) |
| 122 | }) |
| 123 | .chain(key_characteristics.softwareEnforced.into_iter().map(|aidl_kp| { |
| 124 | crate::key_parameter::KeyParameter::new( |
| 125 | KeyParameterValue::convert_from_wire(aidl_kp), |
| 126 | SecurityLevel::SOFTWARE, |
| 127 | ) |
| 128 | })) |
| 129 | .collect() |
| 130 | } |
| 131 | |
| 132 | /// Converts a set of key characteristics from the internal representation into a set of |
| 133 | /// Authorizations as they are used to convey key characteristics to the clients of keystore. |
| 134 | pub fn key_parameters_to_authorizations( |
| 135 | parameters: Vec<crate::key_parameter::KeyParameter>, |
| 136 | ) -> Vec<Authorization> { |
| 137 | parameters.into_iter().map(|p| p.into_authorization()).collect() |
| 138 | } |