blob: b84cb820ffc7e9ae48c8818e0d29ce43f8c14d2b [file] [log] [blame]
Hasini Gunasingheda895552021-01-27 19:34:37 +00001// 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 Danisevskis34a0cf22021-03-08 09:19:03 -080015//! This module implements IKeystoreMaintenance AIDL interface.
Hasini Gunasingheda895552021-01-27 19:34:37 +000016
17use crate::error::map_or_log_err;
18use crate::error::Error as KeystoreError;
Hasini Gunasinghe3ed5da72021-02-04 15:18:54 +000019use crate::globals::{DB, LEGACY_MIGRATOR, SUPER_KEY};
Hasini Gunasingheda895552021-01-27 19:34:37 +000020use crate::permission::KeystorePerm;
21use crate::super_key::UserState;
22use crate::utils::check_keystore_permission;
Janis Danisevskis34a0cf22021-03-08 09:19:03 -080023use android_security_maintenance::aidl::android::security::maintenance::IKeystoreMaintenance::{
24 BnKeystoreMaintenance, IKeystoreMaintenance,
Hasini Gunasingheda895552021-01-27 19:34:37 +000025};
Janis Danisevskis34a0cf22021-03-08 09:19:03 -080026use android_security_maintenance::binder::{Interface, Result as BinderResult};
Janis Danisevskisddd6e752021-02-22 18:46:55 -080027use android_system_keystore2::aidl::android::system::keystore2::Domain::Domain;
Hasini Gunasingheda895552021-01-27 19:34:37 +000028use android_system_keystore2::aidl::android::system::keystore2::ResponseCode::ResponseCode;
29use anyhow::{Context, Result};
30use 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 Danisevskis34a0cf22021-03-08 09:19:03 -080034pub struct Maintenance;
Hasini Gunasingheda895552021-01-27 19:34:37 +000035
Janis Danisevskis34a0cf22021-03-08 09:19:03 -080036impl Maintenance {
Hasini Gunasingheda895552021-01-27 19:34:37 +000037 /// Create a new instance of Keystore User Manager service.
Janis Danisevskis34a0cf22021-03-08 09:19:03 -080038 pub fn new_native_binder() -> Result<Strong<dyn IKeystoreMaintenance>> {
39 let result = BnKeystoreMaintenance::new_binder(Self);
Hasini Gunasingheda895552021-01-27 19:34:37 +000040 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 Gunasinghe3ed5da72021-02-04 15:18:54 +000054 &LEGACY_MIGRATOR,
Hasini Gunasingheda895552021-01-27 19:34:37 +000055 &SUPER_KEY,
56 user_id as u32,
57 password,
58 )
59 })
60 .context("In on_user_password_changed.")?
61 {
62 UserState::LskfLocked => {
Janis Danisevskiseed69842021-02-18 20:04:10 -080063 // Error - password can not be changed when the device is locked
Hasini Gunasingheda895552021-01-27 19:34:37 +000064 Err(KeystoreError::Rc(ResponseCode::LOCKED))
65 .context("In on_user_password_changed. Device is locked.")
66 }
67 _ => {
Janis Danisevskiseed69842021-02-18 20:04:10 -080068 // LskfLocked is the only error case for password change
Hasini Gunasingheda895552021-01-27 19:34:37 +000069 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 Danisevskiseed69842021-02-18 20:04:10 -080078 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 Gunasingheda895552021-01-27 19:34:37 +000088 }
Janis Danisevskisddd6e752021-02-22 18:46:55 -080089
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 Gunasingheda895552021-01-27 19:34:37 +0000100}
101
Janis Danisevskis34a0cf22021-03-08 09:19:03 -0800102impl Interface for Maintenance {}
Hasini Gunasingheda895552021-01-27 19:34:37 +0000103
Janis Danisevskis34a0cf22021-03-08 09:19:03 -0800104impl IKeystoreMaintenance for Maintenance {
Hasini Gunasingheda895552021-01-27 19:34:37 +0000105 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 Danisevskisddd6e752021-02-22 18:46:55 -0800116
117 fn clearNamespace(&self, domain: Domain, nspace: i64) -> BinderResult<()> {
118 map_or_log_err(Self::clear_namespace(domain, nspace), Ok)
119 }
Hasini Gunasingheda895552021-01-27 19:34:37 +0000120}