Janis Danisevskis | 9f10a6a | 2021-01-18 16:45:21 +0000 | [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 IKeyAuthorization AIDL interface. |
| 16 | |
| 17 | use crate::error::map_or_log_err; |
| 18 | use crate::globals::ENFORCEMENTS; |
| 19 | use crate::permission::KeystorePerm; |
| 20 | use crate::utils::check_keystore_permission; |
| 21 | use android_hardware_security_keymint::aidl::android::hardware::security::keymint::{ |
| 22 | HardwareAuthToken::HardwareAuthToken, HardwareAuthenticatorType::HardwareAuthenticatorType, |
Janis Danisevskis | c3a496b | 2021-01-05 10:37:22 -0800 | [diff] [blame] | 23 | }; |
| 24 | use android_hardware_security_secureclock::aidl::android::hardware::security::secureclock::{ |
Janis Danisevskis | 9f10a6a | 2021-01-18 16:45:21 +0000 | [diff] [blame] | 25 | Timestamp::Timestamp, |
| 26 | }; |
| 27 | use android_security_authorization::binder::{Interface, Result as BinderResult}; |
| 28 | use android_security_authorization:: aidl::android::security::authorization::IKeystoreAuthorization::{ |
| 29 | BnKeystoreAuthorization, IKeystoreAuthorization, |
| 30 | }; |
| 31 | use anyhow::{Context, Result}; |
| 32 | use binder::IBinder; |
| 33 | |
| 34 | /// This struct is defined to implement the aforementioned AIDL interface. |
| 35 | /// As of now, it is an empty struct. |
| 36 | pub struct AuthorizationManager; |
| 37 | |
| 38 | impl AuthorizationManager { |
| 39 | /// Create a new instance of Keystore Authorization service. |
| 40 | pub fn new_native_binder() -> Result<impl IKeystoreAuthorization> { |
| 41 | let result = BnKeystoreAuthorization::new_binder(Self); |
| 42 | result.as_binder().set_requesting_sid(true); |
| 43 | Ok(result) |
| 44 | } |
| 45 | |
| 46 | fn add_auth_token(&self, auth_token: &HardwareAuthToken) -> Result<()> { |
| 47 | //check keystore permission |
| 48 | check_keystore_permission(KeystorePerm::add_auth()).context("In add_auth_token.")?; |
| 49 | |
| 50 | //TODO: Keymint's HardwareAuthToken aidl needs to implement Copy/Clone |
| 51 | let auth_token_copy = HardwareAuthToken { |
| 52 | challenge: auth_token.challenge, |
| 53 | userId: auth_token.userId, |
| 54 | authenticatorId: auth_token.authenticatorId, |
| 55 | authenticatorType: HardwareAuthenticatorType(auth_token.authenticatorType.0), |
| 56 | timestamp: Timestamp { milliSeconds: auth_token.timestamp.milliSeconds }, |
| 57 | mac: auth_token.mac.clone(), |
| 58 | }; |
| 59 | ENFORCEMENTS.add_auth_token(auth_token_copy)?; |
| 60 | Ok(()) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | impl Interface for AuthorizationManager {} |
| 65 | |
| 66 | impl IKeystoreAuthorization for AuthorizationManager { |
| 67 | fn addAuthToken(&self, auth_token: &HardwareAuthToken) -> BinderResult<()> { |
| 68 | map_or_log_err(self.add_auth_token(auth_token), Ok) |
| 69 | } |
| 70 | } |