blob: 409feba344e346e77e70787a7bd019d0594d7622 [file] [log] [blame]
Janis Danisevskis9f10a6a2021-01-18 16:45:21 +00001// 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
17use crate::error::map_or_log_err;
18use crate::globals::ENFORCEMENTS;
19use crate::permission::KeystorePerm;
20use crate::utils::check_keystore_permission;
21use android_hardware_security_keymint::aidl::android::hardware::security::keymint::{
22 HardwareAuthToken::HardwareAuthToken, HardwareAuthenticatorType::HardwareAuthenticatorType,
Janis Danisevskisc3a496b2021-01-05 10:37:22 -080023};
24use android_hardware_security_secureclock::aidl::android::hardware::security::secureclock::{
Janis Danisevskis9f10a6a2021-01-18 16:45:21 +000025 Timestamp::Timestamp,
26};
27use android_security_authorization::binder::{Interface, Result as BinderResult};
28use android_security_authorization:: aidl::android::security::authorization::IKeystoreAuthorization::{
29 BnKeystoreAuthorization, IKeystoreAuthorization,
30};
31use anyhow::{Context, Result};
32use binder::IBinder;
33
34/// This struct is defined to implement the aforementioned AIDL interface.
35/// As of now, it is an empty struct.
36pub struct AuthorizationManager;
37
38impl 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
64impl Interface for AuthorizationManager {}
65
66impl 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}