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