blob: 8b7aad9d2f37f1f63acd3ee495c925c82b97d550 [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
15//! This module implements IKeystoreUserManager AIDL interface.
16
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;
23use android_security_usermanager::aidl::android::security::usermanager::IKeystoreUserManager::{
24 BnKeystoreUserManager, IKeystoreUserManager,
25};
26use android_security_usermanager::binder::{Interface, Result as BinderResult};
27use android_system_keystore2::aidl::android::system::keystore2::ResponseCode::ResponseCode;
28use anyhow::{Context, Result};
29use binder::{IBinder, Strong};
30
31/// This struct is defined to implement the aforementioned AIDL interface.
32/// As of now, it is an empty struct.
33pub struct UserManager;
34
35impl 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 Gunasinghe3ed5da72021-02-04 15:18:54 +000053 &LEGACY_MIGRATOR,
Hasini Gunasingheda895552021-01-27 19:34:37 +000054 &SUPER_KEY,
55 user_id as u32,
56 password,
57 )
58 })
59 .context("In on_user_password_changed.")?
60 {
61 UserState::LskfLocked => {
62 //error - password can not be changed when the device is locked
63 Err(KeystoreError::Rc(ResponseCode::LOCKED))
64 .context("In on_user_password_changed. Device is locked.")
65 }
66 _ => {
67 //LskfLocked is the only error case for password change
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.")?;
77 DB.with(|db| UserState::reset_user(&mut db.borrow_mut(), &SUPER_KEY, user_id as u32, false))
78 }
79}
80
81impl Interface for UserManager {}
82
83impl IKeystoreUserManager for UserManager {
84 fn onUserPasswordChanged(&self, user_id: i32, password: Option<&[u8]>) -> BinderResult<()> {
85 map_or_log_err(Self::on_user_password_changed(user_id, password), Ok)
86 }
87
88 fn onUserAdded(&self, user_id: i32) -> BinderResult<()> {
89 map_or_log_err(Self::add_or_remove_user(user_id), Ok)
90 }
91
92 fn onUserRemoved(&self, user_id: i32) -> BinderResult<()> {
93 map_or_log_err(Self::add_or_remove_user(user_id), Ok)
94 }
95}