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 | |
Satya Tangirala | 5b9e5b1 | 2021-03-09 12:54:21 -0800 | [diff] [blame] | 17 | use crate::error::map_km_error; |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 18 | use crate::error::Error as KeystoreError; |
Satya Tangirala | 5b9e5b1 | 2021-03-09 12:54:21 -0800 | [diff] [blame] | 19 | use crate::globals::get_keymint_device; |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 20 | use crate::globals::{DB, LEGACY_MIGRATOR, SUPER_KEY}; |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 21 | use crate::permission::KeystorePerm; |
| 22 | use crate::super_key::UserState; |
| 23 | use crate::utils::check_keystore_permission; |
Janis Danisevskis | 333b7c0 | 2021-03-23 18:57:41 -0700 | [diff] [blame] | 24 | use crate::{database::MonotonicRawTime, error::map_or_log_err}; |
Satya Tangirala | 5b9e5b1 | 2021-03-09 12:54:21 -0800 | [diff] [blame] | 25 | use android_hardware_security_keymint::aidl::android::hardware::security::keymint::IKeyMintDevice::IKeyMintDevice; |
| 26 | use android_hardware_security_keymint::aidl::android::hardware::security::keymint::SecurityLevel::SecurityLevel; |
Hasini Gunasinghe | 9ee1841 | 2021-03-11 20:12:44 +0000 | [diff] [blame] | 27 | use android_security_maintenance::aidl::android::security::maintenance::{ |
| 28 | IKeystoreMaintenance::{BnKeystoreMaintenance, IKeystoreMaintenance}, |
| 29 | UserState::UserState as AidlUserState, |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 30 | }; |
Janis Danisevskis | 34a0cf2 | 2021-03-08 09:19:03 -0800 | [diff] [blame] | 31 | use android_security_maintenance::binder::{Interface, Result as BinderResult}; |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 32 | use android_system_keystore2::aidl::android::system::keystore2::Domain::Domain; |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 33 | use android_system_keystore2::aidl::android::system::keystore2::ResponseCode::ResponseCode; |
| 34 | use anyhow::{Context, Result}; |
Andrew Walbran | 808e860 | 2021-03-16 13:58:28 +0000 | [diff] [blame] | 35 | use binder::{IBinderInternal, Strong}; |
Paul Crowley | f61fee7 | 2021-03-17 14:38:44 -0700 | [diff] [blame] | 36 | use keystore2_crypto::Password; |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 37 | |
| 38 | /// This struct is defined to implement the aforementioned AIDL interface. |
| 39 | /// As of now, it is an empty struct. |
Janis Danisevskis | 34a0cf2 | 2021-03-08 09:19:03 -0800 | [diff] [blame] | 40 | pub struct Maintenance; |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 41 | |
Janis Danisevskis | 34a0cf2 | 2021-03-08 09:19:03 -0800 | [diff] [blame] | 42 | impl Maintenance { |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 43 | /// Create a new instance of Keystore User Manager service. |
Janis Danisevskis | 34a0cf2 | 2021-03-08 09:19:03 -0800 | [diff] [blame] | 44 | pub fn new_native_binder() -> Result<Strong<dyn IKeystoreMaintenance>> { |
| 45 | let result = BnKeystoreMaintenance::new_binder(Self); |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 46 | result.as_binder().set_requesting_sid(true); |
| 47 | Ok(result) |
| 48 | } |
| 49 | |
Paul Crowley | f61fee7 | 2021-03-17 14:38:44 -0700 | [diff] [blame] | 50 | fn on_user_password_changed(user_id: i32, password: Option<Password>) -> Result<()> { |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 51 | //Check permission. Function should return if this failed. Therefore having '?' at the end |
| 52 | //is very important. |
| 53 | check_keystore_permission(KeystorePerm::change_password()) |
| 54 | .context("In on_user_password_changed.")?; |
| 55 | |
Paul Crowley | 7a65839 | 2021-03-18 17:08:20 -0700 | [diff] [blame^] | 56 | if let Some(pw) = password.as_ref() { |
| 57 | DB.with(|db| { |
| 58 | SUPER_KEY.ensure_super_key_created(&mut db.borrow_mut(), user_id as u32, pw) |
| 59 | }) |
| 60 | .context("In on_user_password_changed: ensure_super_key_created failed")?; |
| 61 | } |
| 62 | |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 63 | match DB |
| 64 | .with(|db| { |
| 65 | UserState::get_with_password_changed( |
| 66 | &mut db.borrow_mut(), |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 67 | &LEGACY_MIGRATOR, |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 68 | &SUPER_KEY, |
| 69 | user_id as u32, |
Paul Crowley | f61fee7 | 2021-03-17 14:38:44 -0700 | [diff] [blame] | 70 | password.as_ref(), |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 71 | ) |
| 72 | }) |
| 73 | .context("In on_user_password_changed.")? |
| 74 | { |
| 75 | UserState::LskfLocked => { |
Janis Danisevskis | eed6984 | 2021-02-18 20:04:10 -0800 | [diff] [blame] | 76 | // Error - password can not be changed when the device is locked |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 77 | Err(KeystoreError::Rc(ResponseCode::LOCKED)) |
| 78 | .context("In on_user_password_changed. Device is locked.") |
| 79 | } |
| 80 | _ => { |
Janis Danisevskis | eed6984 | 2021-02-18 20:04:10 -0800 | [diff] [blame] | 81 | // LskfLocked is the only error case for password change |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 82 | Ok(()) |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | fn add_or_remove_user(user_id: i32) -> Result<()> { |
| 88 | // Check permission. Function should return if this failed. Therefore having '?' at the end |
| 89 | // is very important. |
| 90 | check_keystore_permission(KeystorePerm::change_user()).context("In add_or_remove_user.")?; |
Janis Danisevskis | eed6984 | 2021-02-18 20:04:10 -0800 | [diff] [blame] | 91 | DB.with(|db| { |
| 92 | UserState::reset_user( |
| 93 | &mut db.borrow_mut(), |
| 94 | &SUPER_KEY, |
| 95 | &LEGACY_MIGRATOR, |
| 96 | user_id as u32, |
| 97 | false, |
| 98 | ) |
| 99 | }) |
| 100 | .context("In add_or_remove_user: Trying to delete keys from db.") |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 101 | } |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 102 | |
| 103 | fn clear_namespace(domain: Domain, nspace: i64) -> Result<()> { |
| 104 | // Permission check. Must return on error. Do not touch the '?'. |
| 105 | check_keystore_permission(KeystorePerm::clear_uid()).context("In clear_namespace.")?; |
| 106 | |
| 107 | LEGACY_MIGRATOR |
| 108 | .bulk_delete_uid(domain, nspace) |
| 109 | .context("In clear_namespace: Trying to delete legacy keys.")?; |
| 110 | DB.with(|db| db.borrow_mut().unbind_keys_for_namespace(domain, nspace)) |
| 111 | .context("In clear_namespace: Trying to delete keys from db.") |
| 112 | } |
Hasini Gunasinghe | 9ee1841 | 2021-03-11 20:12:44 +0000 | [diff] [blame] | 113 | |
| 114 | fn get_state(user_id: i32) -> Result<AidlUserState> { |
| 115 | // Check permission. Function should return if this failed. Therefore having '?' at the end |
| 116 | // is very important. |
| 117 | check_keystore_permission(KeystorePerm::get_state()).context("In get_state.")?; |
| 118 | let state = DB |
| 119 | .with(|db| { |
| 120 | UserState::get(&mut db.borrow_mut(), &LEGACY_MIGRATOR, &SUPER_KEY, user_id as u32) |
| 121 | }) |
| 122 | .context("In get_state. Trying to get UserState.")?; |
| 123 | |
| 124 | match state { |
| 125 | UserState::Uninitialized => Ok(AidlUserState::UNINITIALIZED), |
| 126 | UserState::LskfUnlocked(_) => Ok(AidlUserState::LSKF_UNLOCKED), |
| 127 | UserState::LskfLocked => Ok(AidlUserState::LSKF_LOCKED), |
| 128 | } |
| 129 | } |
Janis Danisevskis | 333b7c0 | 2021-03-23 18:57:41 -0700 | [diff] [blame] | 130 | |
Satya Tangirala | 5b9e5b1 | 2021-03-09 12:54:21 -0800 | [diff] [blame] | 131 | fn early_boot_ended_help(sec_level: &SecurityLevel) -> Result<()> { |
| 132 | let (dev, _, _) = |
| 133 | get_keymint_device(sec_level).context("In early_boot_ended: getting keymint device")?; |
| 134 | let km_dev: Strong<dyn IKeyMintDevice> = |
| 135 | dev.get_interface().context("In early_boot_ended: getting keymint device interface")?; |
| 136 | map_km_error(km_dev.earlyBootEnded()) |
| 137 | .context("In keymint device: calling earlyBootEnded")?; |
| 138 | Ok(()) |
| 139 | } |
| 140 | |
| 141 | fn early_boot_ended() -> Result<()> { |
| 142 | check_keystore_permission(KeystorePerm::early_boot_ended()) |
| 143 | .context("In early_boot_ended. Checking permission")?; |
| 144 | |
| 145 | let sec_levels = [ |
| 146 | (SecurityLevel::TRUSTED_ENVIRONMENT, "TRUSTED_ENVIRONMENT"), |
| 147 | (SecurityLevel::STRONGBOX, "STRONGBOX"), |
| 148 | ]; |
| 149 | sec_levels.iter().fold(Ok(()), |result, (sec_level, sec_level_string)| { |
| 150 | let curr_result = Maintenance::early_boot_ended_help(sec_level); |
| 151 | if curr_result.is_err() { |
| 152 | log::error!( |
| 153 | "Call to earlyBootEnded failed for security level {}.", |
| 154 | &sec_level_string |
| 155 | ); |
| 156 | } |
| 157 | result.and(curr_result) |
| 158 | }) |
| 159 | } |
| 160 | |
Janis Danisevskis | 333b7c0 | 2021-03-23 18:57:41 -0700 | [diff] [blame] | 161 | fn on_device_off_body() -> Result<()> { |
| 162 | // Security critical permission check. This statement must return on fail. |
| 163 | check_keystore_permission(KeystorePerm::report_off_body()) |
| 164 | .context("In on_device_off_body.")?; |
| 165 | |
| 166 | DB.with(|db| db.borrow_mut().update_last_off_body(MonotonicRawTime::now())) |
| 167 | .context("In on_device_off_body: Trying to update last off body time.") |
| 168 | } |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Janis Danisevskis | 34a0cf2 | 2021-03-08 09:19:03 -0800 | [diff] [blame] | 171 | impl Interface for Maintenance {} |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 172 | |
Janis Danisevskis | 34a0cf2 | 2021-03-08 09:19:03 -0800 | [diff] [blame] | 173 | impl IKeystoreMaintenance for Maintenance { |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 174 | fn onUserPasswordChanged(&self, user_id: i32, password: Option<&[u8]>) -> BinderResult<()> { |
Paul Crowley | f61fee7 | 2021-03-17 14:38:44 -0700 | [diff] [blame] | 175 | map_or_log_err(Self::on_user_password_changed(user_id, password.map(|pw| pw.into())), Ok) |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | fn onUserAdded(&self, user_id: i32) -> BinderResult<()> { |
| 179 | map_or_log_err(Self::add_or_remove_user(user_id), Ok) |
| 180 | } |
| 181 | |
| 182 | fn onUserRemoved(&self, user_id: i32) -> BinderResult<()> { |
| 183 | map_or_log_err(Self::add_or_remove_user(user_id), Ok) |
| 184 | } |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 185 | |
| 186 | fn clearNamespace(&self, domain: Domain, nspace: i64) -> BinderResult<()> { |
| 187 | map_or_log_err(Self::clear_namespace(domain, nspace), Ok) |
| 188 | } |
Hasini Gunasinghe | 9ee1841 | 2021-03-11 20:12:44 +0000 | [diff] [blame] | 189 | |
Janis Danisevskis | 333b7c0 | 2021-03-23 18:57:41 -0700 | [diff] [blame] | 190 | fn getState(&self, user_id: i32) -> BinderResult<AidlUserState> { |
Hasini Gunasinghe | 9ee1841 | 2021-03-11 20:12:44 +0000 | [diff] [blame] | 191 | map_or_log_err(Self::get_state(user_id), Ok) |
| 192 | } |
Janis Danisevskis | 333b7c0 | 2021-03-23 18:57:41 -0700 | [diff] [blame] | 193 | |
Satya Tangirala | 5b9e5b1 | 2021-03-09 12:54:21 -0800 | [diff] [blame] | 194 | fn earlyBootEnded(&self) -> BinderResult<()> { |
| 195 | map_or_log_err(Self::early_boot_ended(), Ok) |
| 196 | } |
| 197 | |
Janis Danisevskis | 333b7c0 | 2021-03-23 18:57:41 -0700 | [diff] [blame] | 198 | fn onDeviceOffBody(&self) -> BinderResult<()> { |
| 199 | map_or_log_err(Self::on_device_off_body(), Ok) |
| 200 | } |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 201 | } |