blob: ad86625e04c04928decf9d4b9a726353630ff3d9 [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::{
23 HardwareAuthToken::HardwareAuthToken, HardwareAuthenticatorType::HardwareAuthenticatorType,
Janis Danisevskisc3a496b2021-01-05 10:37:22 -080024};
25use android_hardware_security_secureclock::aidl::android::hardware::security::secureclock::{
Janis Danisevskis9f10a6a2021-01-18 16:45:21 +000026 Timestamp::Timestamp,
27};
Stephen Crane221bbb52020-12-16 15:52:10 -080028use android_security_authorization::binder::{Interface, Result as BinderResult, Strong};
29use android_security_authorization::aidl::android::security::authorization::IKeystoreAuthorization::{
Janis Danisevskis9f10a6a2021-01-18 16:45:21 +000030 BnKeystoreAuthorization, IKeystoreAuthorization,
31};
Hasini Gunasinghea020b532021-01-07 21:42:35 +000032use android_security_authorization:: aidl::android::security::authorization::LockScreenEvent::LockScreenEvent;
33use android_system_keystore2::aidl::android::system::keystore2::ResponseCode::ResponseCode;
Janis Danisevskis9f10a6a2021-01-18 16:45:21 +000034use anyhow::{Context, Result};
35use binder::IBinder;
36
37/// This struct is defined to implement the aforementioned AIDL interface.
38/// As of now, it is an empty struct.
39pub struct AuthorizationManager;
40
41impl AuthorizationManager {
42 /// Create a new instance of Keystore Authorization service.
Stephen Crane221bbb52020-12-16 15:52:10 -080043 pub fn new_native_binder() -> Result<Strong<dyn IKeystoreAuthorization>> {
Janis Danisevskis9f10a6a2021-01-18 16:45:21 +000044 let result = BnKeystoreAuthorization::new_binder(Self);
45 result.as_binder().set_requesting_sid(true);
46 Ok(result)
47 }
48
49 fn add_auth_token(&self, auth_token: &HardwareAuthToken) -> Result<()> {
50 //check keystore permission
51 check_keystore_permission(KeystorePerm::add_auth()).context("In add_auth_token.")?;
52
53 //TODO: Keymint's HardwareAuthToken aidl needs to implement Copy/Clone
54 let auth_token_copy = HardwareAuthToken {
55 challenge: auth_token.challenge,
56 userId: auth_token.userId,
57 authenticatorId: auth_token.authenticatorId,
58 authenticatorType: HardwareAuthenticatorType(auth_token.authenticatorType.0),
59 timestamp: Timestamp { milliSeconds: auth_token.timestamp.milliSeconds },
60 mac: auth_token.mac.clone(),
61 };
62 ENFORCEMENTS.add_auth_token(auth_token_copy)?;
63 Ok(())
64 }
Hasini Gunasinghea020b532021-01-07 21:42:35 +000065
66 fn on_lock_screen_event(
67 &self,
68 lock_screen_event: LockScreenEvent,
69 user_id: i32,
70 password: Option<&[u8]>,
71 ) -> Result<()> {
72 match (lock_screen_event, password) {
73 (LockScreenEvent::UNLOCK, Some(user_password)) => {
74 //This corresponds to the unlock() method in legacy keystore API.
75 //check permission
76 check_keystore_permission(KeystorePerm::unlock())
77 .context("In on_lock_screen_event: Unlock with password.")?;
78 ENFORCEMENTS.set_device_locked(user_id, false);
79 // Unlock super key.
80 DB.with::<_, Result<()>>(|db| {
81 let mut db = db.borrow_mut();
82 //TODO - b/176123105 - Once the user management API is implemented, unlock is
83 //allowed only if the user is added. Then the two tasks handled by the
84 //unlock_user_key will be split into two methods. For now, unlock_user_key
85 //method is used as it is, which created a super key for the user if one does
86 //not exists, in addition to unlocking the existing super key of the user/
87 SUPER_KEY.unlock_user_key(
88 user_id as u32,
89 user_password,
90 &mut db,
91 &LEGACY_BLOB_LOADER,
92 )?;
93 Ok(())
94 })
95 .context("In on_lock_screen_event.")?;
96
97 Ok(())
98 }
99 (LockScreenEvent::UNLOCK, None) => {
100 check_keystore_permission(KeystorePerm::unlock())
101 .context("In on_lock_screen_event: Unlock.")?;
102 ENFORCEMENTS.set_device_locked(user_id, false);
103 Ok(())
104 }
105 (LockScreenEvent::LOCK, None) => {
106 check_keystore_permission(KeystorePerm::lock())
107 .context("In on_lock_screen_event: Lock")?;
108 ENFORCEMENTS.set_device_locked(user_id, true);
109 Ok(())
110 }
111 _ => {
112 // Any other combination is not supported.
113 Err(KeystoreError::Rc(ResponseCode::INVALID_ARGUMENT))
114 .context("In on_lock_screen_event: Unknown event.")
115 }
116 }
117 }
Janis Danisevskis9f10a6a2021-01-18 16:45:21 +0000118}
119
120impl Interface for AuthorizationManager {}
121
122impl IKeystoreAuthorization for AuthorizationManager {
123 fn addAuthToken(&self, auth_token: &HardwareAuthToken) -> BinderResult<()> {
124 map_or_log_err(self.add_auth_token(auth_token), Ok)
125 }
Hasini Gunasinghea020b532021-01-07 21:42:35 +0000126
127 fn onLockScreenEvent(
128 &self,
129 lock_screen_event: LockScreenEvent,
130 user_id: i32,
131 password: Option<&[u8]>,
132 ) -> BinderResult<()> {
133 map_or_log_err(self.on_lock_screen_event(lock_screen_event, user_id, password), Ok)
134 }
Janis Danisevskis9f10a6a2021-01-18 16:45:21 +0000135}