Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 1 | // Copyright 2021, 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 IKeystoreUserManager AIDL interface. |
| 16 | |
| 17 | use crate::error::map_or_log_err; |
| 18 | use crate::error::Error as KeystoreError; |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 19 | use crate::globals::{DB, LEGACY_MIGRATOR, SUPER_KEY}; |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 20 | use crate::permission::KeystorePerm; |
| 21 | use crate::super_key::UserState; |
| 22 | use crate::utils::check_keystore_permission; |
| 23 | use android_security_usermanager::aidl::android::security::usermanager::IKeystoreUserManager::{ |
| 24 | BnKeystoreUserManager, IKeystoreUserManager, |
| 25 | }; |
| 26 | use android_security_usermanager::binder::{Interface, Result as BinderResult}; |
| 27 | use android_system_keystore2::aidl::android::system::keystore2::ResponseCode::ResponseCode; |
| 28 | use anyhow::{Context, Result}; |
| 29 | use binder::{IBinder, Strong}; |
| 30 | |
| 31 | /// This struct is defined to implement the aforementioned AIDL interface. |
| 32 | /// As of now, it is an empty struct. |
| 33 | pub struct UserManager; |
| 34 | |
| 35 | impl UserManager { |
| 36 | /// Create a new instance of Keystore User Manager service. |
| 37 | pub fn new_native_binder() -> Result<Strong<dyn IKeystoreUserManager>> { |
| 38 | let result = BnKeystoreUserManager::new_binder(Self); |
| 39 | result.as_binder().set_requesting_sid(true); |
| 40 | Ok(result) |
| 41 | } |
| 42 | |
| 43 | fn on_user_password_changed(user_id: i32, password: Option<&[u8]>) -> Result<()> { |
| 44 | //Check permission. Function should return if this failed. Therefore having '?' at the end |
| 45 | //is very important. |
| 46 | check_keystore_permission(KeystorePerm::change_password()) |
| 47 | .context("In on_user_password_changed.")?; |
| 48 | |
| 49 | match DB |
| 50 | .with(|db| { |
| 51 | UserState::get_with_password_changed( |
| 52 | &mut db.borrow_mut(), |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 53 | &LEGACY_MIGRATOR, |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 54 | &SUPER_KEY, |
| 55 | user_id as u32, |
| 56 | password, |
| 57 | ) |
| 58 | }) |
| 59 | .context("In on_user_password_changed.")? |
| 60 | { |
| 61 | UserState::LskfLocked => { |
Janis Danisevskis | eed6984 | 2021-02-18 20:04:10 -0800 | [diff] [blame^] | 62 | // Error - password can not be changed when the device is locked |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 63 | Err(KeystoreError::Rc(ResponseCode::LOCKED)) |
| 64 | .context("In on_user_password_changed. Device is locked.") |
| 65 | } |
| 66 | _ => { |
Janis Danisevskis | eed6984 | 2021-02-18 20:04:10 -0800 | [diff] [blame^] | 67 | // LskfLocked is the only error case for password change |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 68 | Ok(()) |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | fn add_or_remove_user(user_id: i32) -> Result<()> { |
| 74 | // Check permission. Function should return if this failed. Therefore having '?' at the end |
| 75 | // is very important. |
| 76 | check_keystore_permission(KeystorePerm::change_user()).context("In add_or_remove_user.")?; |
Janis Danisevskis | eed6984 | 2021-02-18 20:04:10 -0800 | [diff] [blame^] | 77 | DB.with(|db| { |
| 78 | UserState::reset_user( |
| 79 | &mut db.borrow_mut(), |
| 80 | &SUPER_KEY, |
| 81 | &LEGACY_MIGRATOR, |
| 82 | user_id as u32, |
| 83 | false, |
| 84 | ) |
| 85 | }) |
| 86 | .context("In add_or_remove_user: Trying to delete keys from db.") |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
| 90 | impl Interface for UserManager {} |
| 91 | |
| 92 | impl IKeystoreUserManager for UserManager { |
| 93 | fn onUserPasswordChanged(&self, user_id: i32, password: Option<&[u8]>) -> BinderResult<()> { |
| 94 | map_or_log_err(Self::on_user_password_changed(user_id, password), Ok) |
| 95 | } |
| 96 | |
| 97 | fn onUserAdded(&self, user_id: i32) -> BinderResult<()> { |
| 98 | map_or_log_err(Self::add_or_remove_user(user_id), Ok) |
| 99 | } |
| 100 | |
| 101 | fn onUserRemoved(&self, user_id: i32) -> BinderResult<()> { |
| 102 | map_or_log_err(Self::add_or_remove_user(user_id), Ok) |
| 103 | } |
| 104 | } |