blob: fbaa9ebbe2d4f01ae88fbbb420ff7d0cb8b53ace [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
Hasini Gunasinghe0e161452021-01-27 19:34:37 +000015//! This module implements IKeystoreAuthorization AIDL interface.
Janis Danisevskis9f10a6a2021-01-18 16:45:21 +000016
Hasini Gunasinghea020b532021-01-07 21:42:35 +000017use crate::error::Error as KeystoreError;
Janis Danisevskis9f10a6a2021-01-18 16:45:21 +000018use crate::error::map_or_log_err;
Hasini Gunasinghea020b532021-01-07 21:42:35 +000019use crate::globals::{DB, ENFORCEMENTS, LEGACY_BLOB_LOADER, SUPER_KEY};
Janis Danisevskis9f10a6a2021-01-18 16:45:21 +000020use crate::permission::KeystorePerm;
21use crate::utils::check_keystore_permission;
22use android_hardware_security_keymint::aidl::android::hardware::security::keymint::{
Hasini Gunasingheda895552021-01-27 19:34:37 +000023 HardwareAuthToken::HardwareAuthToken,
Janis Danisevskis9f10a6a2021-01-18 16:45:21 +000024};
Stephen Crane221bbb52020-12-16 15:52:10 -080025use android_security_authorization::binder::{Interface, Result as BinderResult, Strong};
26use android_security_authorization::aidl::android::security::authorization::IKeystoreAuthorization::{
Janis Danisevskis9f10a6a2021-01-18 16:45:21 +000027 BnKeystoreAuthorization, IKeystoreAuthorization,
28};
Hasini Gunasinghea020b532021-01-07 21:42:35 +000029use android_security_authorization:: aidl::android::security::authorization::LockScreenEvent::LockScreenEvent;
30use android_system_keystore2::aidl::android::system::keystore2::ResponseCode::ResponseCode;
Janis Danisevskis9f10a6a2021-01-18 16:45:21 +000031use 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.
Stephen Crane221bbb52020-12-16 15:52:10 -080040 pub fn new_native_binder() -> Result<Strong<dyn IKeystoreAuthorization>> {
Janis Danisevskis9f10a6a2021-01-18 16:45:21 +000041 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
Hasini Gunasingheda895552021-01-27 19:34:37 +000050 ENFORCEMENTS.add_auth_token(auth_token.clone())?;
Janis Danisevskis9f10a6a2021-01-18 16:45:21 +000051 Ok(())
52 }
Hasini Gunasinghea020b532021-01-07 21:42:35 +000053
54 fn on_lock_screen_event(
55 &self,
56 lock_screen_event: LockScreenEvent,
57 user_id: i32,
58 password: Option<&[u8]>,
59 ) -> Result<()> {
60 match (lock_screen_event, password) {
61 (LockScreenEvent::UNLOCK, Some(user_password)) => {
62 //This corresponds to the unlock() method in legacy keystore API.
63 //check permission
64 check_keystore_permission(KeystorePerm::unlock())
65 .context("In on_lock_screen_event: Unlock with password.")?;
66 ENFORCEMENTS.set_device_locked(user_id, false);
67 // Unlock super key.
68 DB.with::<_, Result<()>>(|db| {
69 let mut db = db.borrow_mut();
70 //TODO - b/176123105 - Once the user management API is implemented, unlock is
71 //allowed only if the user is added. Then the two tasks handled by the
72 //unlock_user_key will be split into two methods. For now, unlock_user_key
73 //method is used as it is, which created a super key for the user if one does
74 //not exists, in addition to unlocking the existing super key of the user/
75 SUPER_KEY.unlock_user_key(
Hasini Gunasingheda895552021-01-27 19:34:37 +000076 &mut db,
Hasini Gunasinghea020b532021-01-07 21:42:35 +000077 user_id as u32,
78 user_password,
Hasini Gunasinghea020b532021-01-07 21:42:35 +000079 &LEGACY_BLOB_LOADER,
80 )?;
81 Ok(())
82 })
83 .context("In on_lock_screen_event.")?;
84
85 Ok(())
86 }
87 (LockScreenEvent::UNLOCK, None) => {
88 check_keystore_permission(KeystorePerm::unlock())
89 .context("In on_lock_screen_event: Unlock.")?;
90 ENFORCEMENTS.set_device_locked(user_id, false);
91 Ok(())
92 }
93 (LockScreenEvent::LOCK, None) => {
94 check_keystore_permission(KeystorePerm::lock())
95 .context("In on_lock_screen_event: Lock")?;
96 ENFORCEMENTS.set_device_locked(user_id, true);
97 Ok(())
98 }
99 _ => {
100 // Any other combination is not supported.
101 Err(KeystoreError::Rc(ResponseCode::INVALID_ARGUMENT))
102 .context("In on_lock_screen_event: Unknown event.")
103 }
104 }
105 }
Janis Danisevskis9f10a6a2021-01-18 16:45:21 +0000106}
107
108impl Interface for AuthorizationManager {}
109
110impl IKeystoreAuthorization for AuthorizationManager {
111 fn addAuthToken(&self, auth_token: &HardwareAuthToken) -> BinderResult<()> {
112 map_or_log_err(self.add_auth_token(auth_token), Ok)
113 }
Hasini Gunasinghea020b532021-01-07 21:42:35 +0000114
115 fn onLockScreenEvent(
116 &self,
117 lock_screen_event: LockScreenEvent,
118 user_id: i32,
119 password: Option<&[u8]>,
120 ) -> BinderResult<()> {
121 map_or_log_err(self.on_lock_screen_event(lock_screen_event, user_id, password), Ok)
122 }
Janis Danisevskis9f10a6a2021-01-18 16:45:21 +0000123}