blob: 9ee672716d08644051c4539ed7d3751952d9d03c [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;
19use crate::globals::{DB, SUPER_KEY};
20use 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(),
53 &SUPER_KEY,
54 user_id as u32,
55 password,
56 )
57 })
58 .context("In on_user_password_changed.")?
59 {
60 UserState::LskfLocked => {
61 //error - password can not be changed when the device is locked
62 Err(KeystoreError::Rc(ResponseCode::LOCKED))
63 .context("In on_user_password_changed. Device is locked.")
64 }
65 _ => {
66 //LskfLocked is the only error case for password change
67 Ok(())
68 }
69 }
70 }
71
72 fn add_or_remove_user(user_id: i32) -> Result<()> {
73 // Check permission. Function should return if this failed. Therefore having '?' at the end
74 // is very important.
75 check_keystore_permission(KeystorePerm::change_user()).context("In add_or_remove_user.")?;
76 DB.with(|db| UserState::reset_user(&mut db.borrow_mut(), &SUPER_KEY, user_id as u32, false))
77 }
78}
79
80impl Interface for UserManager {}
81
82impl IKeystoreUserManager for UserManager {
83 fn onUserPasswordChanged(&self, user_id: i32, password: Option<&[u8]>) -> BinderResult<()> {
84 map_or_log_err(Self::on_user_password_changed(user_id, password), Ok)
85 }
86
87 fn onUserAdded(&self, user_id: i32) -> BinderResult<()> {
88 map_or_log_err(Self::add_or_remove_user(user_id), Ok)
89 }
90
91 fn onUserRemoved(&self, user_id: i32) -> BinderResult<()> {
92 map_or_log_err(Self::add_or_remove_user(user_id), Ok)
93 }
94}