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