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